Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is wrong with this script?

Asked by 11 years ago
01local debounce = false
02 
03function getPlayer(humanoid)
04local players = game.Players:children()
05for i = 1, #players do
06if players[i].Character.Humanoid == humanoid then return players[i] end -- The error was on this line
07end
08return nil
09end
10 
11function onTouch(part)
12 
13local human = part.Parent:findFirstChild("Humanoid")
14if (human ~= nil) and debounce == false then
15 
View all 30 lines...

1 answer

Log in to vote
1
Answered by 11 years ago

Instead of this:

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

You could just do this:

1function getPlayer(humanoid)
2    return game.Players:GetPlayerFromCharacter(humanoid.Parent) --This gets the player from the character and returns that player or returns nil if the player doesn't exist
3end

It's much simpler. I hope this helped!

*Note: for more information on :GetPlayerFromCharacter(), click this: GetPlayerFromCharacter

Ad

Answer this question