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

Why does roblox studio say that 'Character' is a nil value?

So I have a script:

game.Players.PlayerAdded:connect(function(plr)
	print("1")
	local clone = game.ReplicatedStorage.Helmet:Clone()
	print("2")
	clone.Parent = plr.Character
	print("3")
	print(plr.Name)
	print(plr.Character.Name)
end)

It prints 1,2 and 3. It prints my username for plr.Name, but for plr,Character, it says: "Workspace.Script:8: attempt to index field 'Character' (a nil value)"

1 answer

✓ Accepted answer

###This is because when a player joins, the character doesn't load in instantly. You'll need to wait for the character, by using a CharacterAdded event.

local helmet = game:GetService("ReplicatedStorage"):WaitForChild("Helmet")
-- place in server storage if you don't want exploiters freely giving themselves the helmet

game:GetService("Players").PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        print(1)
        local clone helmet:Clone()
        print(2)
        clone.Parent = char
        print(3)
    end)

    print(plr.Name)
end)

###On a side note, switch to :Connect(), as :connect() is deprecated and should not be used in new work. Learn more here.

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