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

LocalPlayer.Character, a nil value. Why?

Asked by 4 years ago
Edited 4 years ago

I made a button that's supposed to lead your character of a cliff, but it didn't work. This is the code I used.

plr = game.Players.LocalPlayer
char = plr.Character
click = game.Workspace.DieButton.Button.ClickDetector
click.MouseClick:Connect(function()
    char.Humanoid:MoveTo(game.Workspace.Die.Position)
end)

and it came out with an error: Players.marioman1932.PlayerScripts.Die:5: attempt to index global 'char' (a nil value) but it's not a nil value because I made a variable for it. Please help of why it says that. Edit: Yes this is in a local script.

0
I think it depends if you're using a localscript, or a script. If this is a localscript, it has to work. If you are using a normal script, try click.MouseClick:Connect(function(player) BiIinear 104 — 4y
0
sorry i didnt read the edit, i honestly dont know why it isnt working BiIinear 104 — 4y
1
This is because you need to wait for the character to load OBenjOne 190 — 4y
0
How do I do that? (post it in the answer bar) marioman1932 48 — 4y
0
I did but deleted it because @psudar's is better OBenjOne 190 — 4y

1 answer

Log in to vote
1
Answered by
Psudar 882 Moderation Voter
4 years ago
Edited 4 years ago

I think whats happening here is either:

A) The character hasnt been loaded in;

B) You might not be running this in a local script

--local script
plr = game.Players.LocalPlayer
char = plr.Character or plr.CharacterAdded:Wait()

click = game.Workspace.DieButton.Button.ClickDetector
click.MouseClick:Connect(function()
    char.Humanoid:MoveTo(game.Workspace.Die.Position)
end)

The reason this works is because or turns this into a conditional statement. If Player.Character comes back as nil, it goes to the next condition, which is the CharacterAdded event. It then halts the script from continuing until theres a character loaded in.

Hopefully that helps.

https://developer.roblox.com/en-us/api-reference/property/Player/Character

0
Line two of his script would error if it's not a LocalScript. That's clearly not his problem. User#26971 0 — 4y
1
Thanks! I like how you also explained why it worked! marioman1932 48 — 4y
0
Oof no problem. Also I didnt really think about line 1, its clearly a local script because it errors at line 2. Oversight on my part, most people would probably make the same mistake. Oh well, glad I helped you out. Psudar 882 — 4y
Ad

Answer this question