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

Why does my script not work?

Character is obviously part of Player, however it is giving me the error message that "Character is a nil value". Help me out?

game.Players.PlayerAdded:connect(function(plr)
	local uni = game.ServerStorage.Union:Clone()
	local head = plr.Character.Head
	local weld = Instance.new("Weld", uni)
	uni.Parent = plr.Character
	uni.Anchored = false
	weld.Part0 = head
	weld.C0 = head.CFrame:inverse()
	weld.Part1 = plr.Character
	weld.C1 = plr.Character.CFrame:inverse()
	
end)

1 comment

1

It's likely the script is loading before the player, between lines 2 and 3 add the following line; plr.CharacterAdded:wait() --This will tell the script to wait until the event CharacterAdded fires.

1 answer

✓ Accepted answer

Your Problem

As M39a9am3R commented, your problem is that the code is firing prior to the Character ever loading. The Character IS nil when your script reads line 3, so you need to wait until the character has loaded.

To do this, you can use the CharacterAdded event which fires upon the Character being added to the workspace. Use the wait method to wait until the next time the event fires;

plr.CharacterAdded:wait()


Code

game.Players.PlayerAdded:connect(function(plr)
	local uni = game.ServerStorage.Union:Clone()
	plr.CharacterAdded:wait() --Wait for it to load!
	local head = plr.Character.Head
	local weld = Instance.new("Weld", uni)
	uni.Parent = plr.Character
	uni.Anchored = false
	weld.Part0 = head
	weld.C0 = head.CFrame:inverse()
	weld.Part1 = plr.Character
	weld.C1 = plr.Character.CFrame:inverse()
end)

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