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

What is wrong with this script?

local debounce = false

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

function onTouch(part) 

local human = part.Parent:findFirstChild("Humanoid") 
if (human ~= nil) and debounce == false then

debounce = true

local player = getPlayer(human) 

if (player == nil) then return end 

script.Parent:clone().Parent = player.Backpack

wait(2)
debounce = false
end
end


script.Parent.Parent.Touched:connect(onTouch) 

1 answer

Instead of this:

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

You could just do this:

function getPlayer(humanoid)
	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
end

It's much simpler. I hope this helped!

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

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