Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I don't understand this script, could you please help out and explain what it does and how?

function getPlayer(humanoid) 
local players = game.Players:children() 
for i = 1, #players do 
if players[i].Character.Humanoid == humanoid then return players[i] end 
end 
return nil 
end 

I don't understand a lot of this script, I understand the first two lines but the rest i don't understand (I especially don't understand the i and # and return) Thank you guys so much!

1 answer

✓ Accepted answer

The # returns the length of a table or string. Example:

local words = {"Hi", "Hello", "How are you"}

print(#words) --> 3
-- It prints 3 because there are three items in the table.

local word = "Octopus"

print(#word) --> 7

-- Returns 7 because there are 7 letters in "Octopus".

Now, the i is just the name for the variable. It can be anything.

But overall what the script does is if the humanoid is equal to the parameter provided, it will return the player.

Going back to the #words thing. To access a specific item in a table, you get it from its place in the table. As you can see, the first item in the table was "Hi". So if we do this:

print(words[1]) --> Hi

-- Hi was the first thing on the table, saying [2] returns hello. And so on.

What the "for i = 1, #players do" part is doing is looping through the "#players".

The i goes from number 1, to how many players are in the server. The players[i] is the player. "i" being their number in the table. Hope it helps.

1 comment

0

Thank you so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.