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

Player variable a nil value? [SOLVED]

Asked by 5 years ago
Edited 5 years ago
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.

0
use an if statement before defining the variable DeceptiveCaster 3761 — 5y
0
what do you mean? utrabem 5 — 5y
0
"if game.Players.LocalPlayer ~= nil then" DeceptiveCaster 3761 — 5y
0
then define the variable DeceptiveCaster 3761 — 5y

1 answer

Log in to vote
1
Answered by
OfcPedroo 396 Moderation Voter
5 years ago
Edited 5 years ago

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

1
No, don't do that. Don't loop. User#19524 175 — 5y
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... OfcPedroo 396 — 5y
0
I'll try that, if it don't work, i'll keep waiting for answers utrabem 5 — 5y
0
If it works, please mark as solved. :P OfcPedroo 396 — 5y
View all comments (12 more)
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 User#19524 175 — 5y
0
Also why do you repeat wait until a service? User#19524 175 — 5y
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 OfcPedroo 396 — 5y
0
i agree with @incapaz DeceptiveCaster 3761 — 5y
0
@OfcPedroo no, it did not. Please do not suggest bad ideas User#19524 175 — 5y
0
It didn't work aswell.. utrabem 5 — 5y
0
lol User#19524 175 — 5y
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.. OfcPedroo 396 — 5y
0
Well looping until something exists is not a good idea. User#19524 175 — 5y
0
Ok, thanks for the advice. I will research that later and find a better way of replacing it. OfcPedroo 396 — 5y
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 theking48989987 2147 — 5y
0
^theking is right, and it is the same with services as well Vulkarin 581 — 5y
Ad

Answer this question