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

Attempt To Index Character (a nil value)?

Here is the code

local plr = script.Parent.Parent
plr.Character:FindFirstChild("Humanoid").Parent:Destroy()
local characterbod = game.ReplicatedStorage.Test:Clone()
characterbod.Name = script.Parent.Parent.Name
characterbod.Parent = workspace
characterbod:MoveTo(workspace.Part.Position)
local cam = workspace.CurrentCamera
plr.Character = characterbod


On line two, the error says that character is a nil value. PLEASE HELP ME

Fixed: Here is the better one.

4 comments

0

Can you show the heirarchy to make sure "script.Parent.Parent" is actually the player?

1

Add "repeat wait() until plr.Character and plr.Character.Parent" right after you define "plr". The reason for this is because the character doesn't exist the second the player spawns, so it needs to wait for it. Also change line 2 to just "plr.Character = nil" Setting Character to nil just removes the character as well as setting the character to nil.

0

I was thinking that too @Tkdriverx, Also I figured given server scripts take longer to load and run I thought that the character would have loaded by then? (As opposed to local scripts that go right away and you need to use WaitForChild all the time)

0

It still doesn't load quick enough to get the Character the second it spawns. The GUI and Backpack usually load before the character does.

1 answer

Just simply add

repeat
	wait()
until plr.Character and plr.Character.Parent

right after you define 'plr'.

Also change line 2 (the current line 2) to plr.Character = nil Setting the character to nil also removes the character.

4 comments

0

Why wait for plr.Character.Parent? That would be Workspace

0

That's to make sure the character has a parent before continuing. If you don't, it might get the character right before it's given a parent, and weird things will happen.

0

Oh, alright

0

/thanks for the help but I added a more updated version

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