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

Am I using FindFirstChild correctly?

I am creating a script that will detect if a given player's name is in a list of tools, and then give the tool with the player's name to that player on joining and respawning.

It has not been working(no tool given to player upon joining or respawning). Here is the code.

game.Players.PlayerAdded:Connect(function(plr)
local playername= game.Workspace:WaitForChild(plr.Name)
	if game.ServerStorage.PlayerTools.FindFirstChild(playername) ~= nil then --checks if the tool exists
		game.ServerStorage.PlayerTools.FindFirstChild(playername):Clone().Parent = plr.Backpack --clones and places tool in backpack
		plr.CharacterAdded:Connect(function() --creates function that gives tool back on respawn
			game.ServerStorage.PlayerTools.FindFirstChild(character):Clone().Parent = plr.Backpack
		end)
	end
end)

A previous iteration of this script checked for a specific player name and then gave that specific tool to the player, using a similar structure to this. It worked.

I think the problem here either has to do with my use of FindFirstChild or some dumb logic error I made. I checked the documentation and .Name is a string, so FindFirstChild be working, but since this is my first time working in lua in a while I could be forgetting something.

1 comment

0

i don't think you would use "FindFirstChild" to get a playername. maybe you could "FindFirstChild" for their character first, and then get their name normally?

1 answer

✓ Accepted answer

You need to use a : for findfirstchild(), like this

game.Players.PlayerAdded:Connect(function(plr)
local playername= game.Workspace:WaitForChild(plr.Name)
    if game.ServerStorage.PlayerTools:FindFirstChild(playername) ~= nil then --checks if the tool exists
        game.ServerStorage.PlayerTools:FindFirstChild(playername):Clone().Parent = plr.Backpack --clones and places tool in backpack
        plr.CharacterAdded:Connect(function() --creates function that gives tool back on respawn
            game.ServerStorage.PlayerTools:FindFirstChild(character):Clone().Parent = plr.Backpack
        end)
    end
end)

game:FindFirstChild() like that

I fixed your script btw

1 comment

0

Thanks!

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