Attempt to index nil with "WaitForChild"

My issue:
Im trying to give the players a folder called “PlayerVariables” if it doesn’t already exist in their character. Here’s the code:

local players = game:GetService("Players")

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(plr)
	if plr.Character:FindFirstChild("PlayerVariables") == nil then
		return true
	else
		local vars = game.ReplicatedStorage.PlayerVariables:Clone()
		vars.Parent = plr.Character
	end
end)
1 Like

I don’t see a waitforchild here, which line error’d?

You did not use characteradded.

Heres code should help/fix your issue

local plrs = game:GetService('Players')

plrs.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local PlayerVariablesRep = game:GetService('ReplicatedStorage').PlayerVariables:Clone()
        if character:FindFirstChild('PlayerVariables') then
            return "Found Folder"
        else
            PlayerVariablesRep.Parent = character
        end
    end)
end)
2 Likes

I meant to say “FindFirstChild” My mistake, however my friend fixed it.

2 Likes