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

Attempt To Index Character (a nil value)?

Asked by 10 years ago

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.

0
Can you show the heirarchy to make sure "script.Parent.Parent" is actually the player? VariadicFunction 335 — 10y
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. Tkdriverx 514 — 10y
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) VariadicFunction 335 — 10y
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. Tkdriverx 514 — 10y

1 answer

Log in to vote
1
Answered by
Tkdriverx 514 Moderation Voter
10 years ago

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.

0
Why wait for plr.Character.Parent? That would be Workspace VariadicFunction 335 — 10y
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. Tkdriverx 514 — 10y
0
Oh, alright VariadicFunction 335 — 10y
0
/thanks for the help but I added a more updated version ApolloTalentz 20 — 10y
Ad

Answer this question