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

Am I using FindFirstChild correctly?

Asked by 4 years ago

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.

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? DemonHunterz6 35 — 4y

1 answer

Log in to vote
0
Answered by
zomspi 541 Moderation Voter
4 years ago

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

0
Thanks! Perimetric 2 — 4y
Ad

Answer this question