LocalPlayer is a nil value..?

I’m making it so that when you click a button, a certain animation plays. I’ve gone through much trouble and time trying to achieve this, and finally, I’ve got it. Only in studio though…

When I play in Roblox Studio, the button works and plays the animation. I then published it, made it active, then went into the game. When I pressed the button, nothing happened.

I looked in the dev console, and it showed an error that said something along the lines of “Attempt to index nil value ‘LocalPlayer’”.

I’m not really sure how I can make this work, I’ve tried putting it in a Localscript, that doesn’t work either though. Anyways, here’s the script:

image
(in a regular script)

And the path of the script:

image

Thanks for the help! :slightly_smiling_face:

5 Likes

Players.LocalPlayer can only be from the client, the reason why this works in Play Solo is because your computer “acts” as both the client and the server. If you were to test this with Start Server you would incounter this bug as well.

You must change the (Server)Scripts to LocalScripts, you could put them in StarterPlayer.StarterCharacterScripts and then the following should work.

local Humanoid = script.Parent:WaitForChild("Humanoid")
-- ...
-- rest of code
-- ...

A quick glance tells me the code should work with that sole change, assuming both scripts are changed and it’s ok for it to only interact locally per-player.

Also, screenshoting code is not the best method, you should directly paste your code and format it like this:

```lua
YOUR CODE HERE
```

Edit: I assume it didn’t work for you because you didn’t put them in a container that would run locally, which I just tested in Start Server and that’s the case (I put a LocalScript in both Workspace and StarterPlayerScripts and only one printed)

13 Likes

Thank you, your solution did work. It will make scripting multiple buttons more tedious, but it might have to do.
If you have any suggestions as to how i could make it so I could duplicate the button so that it would work, I would really appreciate it. It is fine if not though. Thank you.

Here’s a tip to make sure you never mess up again: never ever EVER put a Script in anything related to GUIs.

8 Likes

Alright thanks for the tip! That should be useful in the future.

Yeah, local scripts are meant for the local client, and server scripts are for the server. You’d never want to use server scripts for anything that only the player will see. You’d use server scripts in someplace like workspace or serverscriptservice to control something everyone will see, for example, walk speed or transparency.

1 Like

Also, just in case this ever happens to you, sometimes even in a local script the Character object under player will not be loaded when the script is run. Player.Character == nil

I have seen in the core scripts where they will use some sort of detection or waiting for the Character object to be non nil

So just something you might want to look out for.

If I recall correctly, this is no longer the case for localscripts. LocalPlayer will always be non-nil.

CoreScripts run before LocalScripts are executed, so it might be nil for them. Though last I heard this was getting changed(?)

Okay so a new problem has risen. This is when I attach it to a door system. It works in studio but not in a server. I’ve looked and put print() things to check if everything works, and it does. The door doesn’t move at all though.

No, never. Put those anywhere else. Why are you calling Kick from a GUI, stop.

4 Likes

Yeah, if you’re kicking players from a sever script in a GUI, they can easily delete said script and therefore not be kicked.

That is what remotes are used for. GUI scripts should only ever interpret user input and pass it off to the server to check whenever sensitive server data is involved.

1 Like

Don’t use remotes to kick the player. This will only fail. Kick the player in server scripts that handle validation. Failing client side checks should give your cheating pal a friendly crash.

1 Like

Sorry, I didn’t really say what I meant very well. What you said is what I meant. Treat UI actions as input and pass it off to the server as a request and have the server check/validate before performing the action itself. Also, I mentioned sensitive data because it applies to more than just kicking, but for things like purchases as well.

1 Like

Perhaps I didn’t make myself very clear, LocalPlayer is not the issue, its the Character object under LocalPlayer.
being nil, even after the script is running (a script that is parented to Character)

I ran into this issue just a few weeks ago.
I thought at first that it was something I might be doing wrong, but if you look in the scripts that come with Roblox Gear (such as weapons, and vehicles) you will see where they wait to detect for Character to be non nil.

You can’t find local player from a script that is not a local script. Instead use local player = game.Players:GetPlayerFromCharacter(script.Parent)