The role of the Holy Spirit is one that is difficult to obtain. In the New Testament, of course, we realize that the Holy Spirit can indwell people, but that's only because we have been made perfectly clean by Jesus Christ, through His righteousness, through His perfection imputed to us. Now that that's happened and Christ has ascended the right hand of God, the Father, and He has our great high priest making intercession for us, He now gives us the Holy Spirit because we are made clean through Him. In the Old Testament, Christ had not finished His work yet. Thus, the Holy Spirit could not indwell any, but He could come upon. In the Old Testament, specifically in those two books that you reference, the Holy Spirit comes upon those people in which God tasks with a certain job.
As you think about King Saul for a moment, when the people ask God for a king just like a king from all the other nations, God say, "Okay, I'm going to give you a king just like the other nations, and how will I prove to you who that King is. I will place my Holy Spirit upon him." Saul then goes in and he does something that he doesn't do before. He prophesies among the prophets. Thus, evidencing and showing everyone around, "This is the man that I have called for this task." Immediately after, it's two or three chapters later, Saul transgresses his title. In other words, he's a king that goes in and does the work of a prophet, which he cannot do. What happens to him? The Holy Spirit immediately leaves him.
God takes that spirit off of him, and he places it upon David, and it's evidence to show this one was the king, no longer the king, and now this one is my king. That's why later in the book of Psalms, David, after he had sinned pleas with God, "Please do not remove your Holy Spirit from me." He's not praying for salvation or loss of salvation. What's he praying is, "Do not remove the kingdom from me."