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

Player variable a nil value? [SOLVED]

local player = game:GetService("Players").LocalPlayer

This is the script i use for player variables, somehow when i test with emulator it says its a nil value, althrough i tried with wait(0.1), and repeat wait() until player ~= nil, but it keeps doing that.

4 comments

0

use an if statement before defining the variable

0

what do you mean?

0

"if game.Players.LocalPlayer ~= nil then"

0

then define the variable

1 answer

✓ Accepted answer

First of all, LocalPlayer should only be used on LocalScripts. If you're using with Scripts, you might want to make this:

game.Players.PlayerAdded:connect(function(player)
	-- actions with the player
end)

If you're using actual LocalScripts, try this:

repeat wait() until game.Players
repeat wait() until game.Players.LocalPlayer

-- actions with localplayer

This always works for me. Test and let me know something :P

16 comments

1

No, don't do that. Don't loop.

0

On the past I already made things without loop about the LocalPlayer, and it didn't work. After that, I tried with the repeat wait() and it worked...

0

I'll try that, if it don't work, i'll keep waiting for answers

0

If it works, please mark as solved. :P

1

No, it won't. @OP don't do that. That's bad. Use events instead. Heck you don't need events nor loops for the LocalPlayer

0

Also why do you repeat wait until a service?

0

It solved things for me... Perhaps Events are a better way of doing it. But repeat wait() did the job for me most times

0

i agree with @incapaz

0

@OfcPedroo no, it did not. Please do not suggest bad ideas

0

It didn't work aswell..

0

lol

0

I really mean, there were sometimes that repeat wait() DID the job for me. Perhaps there are better solutions. I'm still an apprentice, and I gave what I thought the solution is. That already worked for me. If you guys have any idea, you should try and grab the solution..

0

Well looping until something exists is not a good idea.

0

Ok, thanks for the advice. I will research that later and find a better way of replacing it.

0

Um, no matter where a local script is located in the client, whether that be the starter character script, starter pack, etc. the player existed before the local script was cloned into the player. Therefore you can just do game.Players.LocalPlayer and it should work 100% of the time in a local script

0

^theking is right, and it is the same with services as well

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