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

How to get a players character without "attempted to index nil with 'Character'"?

Asked by 4 years ago

This is really annoying me. I was trying to make the player jump in a script when they clicked a button, but output just says "attempt to index nil with 'Character'". It's in a server side script, and the local player had already been addressed and everything. I have tried this:

lp = game.Players.LocalPlayer
lpc = lp.Character

and this:

lpc = game.Players.LocalPlayer.Character

also this...

lp = game.Players.LocalPlayer
lpc = game.Workspace:FindFirstChild(lp.Name)

this was the code for the jump... (inside of a function)

lpc.Humanoid.Jump = true

Why does this keep happening? Why does it work in scripts other than mine? I'm only getting errors for this part of the script. If you need me to release the whole script, I will.

0
You can't call localplayer from a server script. Is the button a gui or a click detector? ultrasonicboomer 85 — 4y
0
clickdetector AbsurdAwesome101 56 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

Clickdetectors have an argument in the MouseClick event that passes the player that clicked it. From there you can call their character and humanoid.

--server script

Part = script.Parent

function onClick(plr)
 plr.Character.Humanoid.Jump = true
end

Part.ClickDetector.MouseClick:Connect(onClick)
Ad
Log in to vote
0
Answered by
Raccoonyz 1092 Donator Moderation Voter
4 years ago

You cannot use the local player in a server script - only in a local script.

To get around this, you can use remote events.

Create a remote event in the workspace with a server script listening to it. Have a button that fires the event on the client, and you've got yourself a working jump button.

Answer this question