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

Is LocalPlayer not viable anymore?

Asked by 5 years ago

For some reason this simple script is not working and I have no idea why. What am I missing?

local player = game.Players.LocalPlayer.leaderstats.Level.Value
repeat 
wait(10)


if player == 1 then
wait(0.1)
script.Parent.Text = "Private I (Lvl 1)"
elseif player == 2 then
wait(0.1)
script.Parent.Text = "Private II (Lvl 2)"
elseif player == 3 then
wait(0.1)
script.Parent.Text = "Private III (Lvl 3)"

It is kinda ridiculous that this script works while LocalPlayer does not

script.Parent.Parent.Parent.Parent.Parent.leaderstats.Level.Value

Btw, this script is located at starterGUI

0
Local Player only works in a Local Script Zikelah 20 — 5y
0
I am able to use game.Players.LocalPlayer in other script so it does work. Its just this one won't work Asher0606 36 — 5y
0
why is there a repeat at the top, and if its on purpose where is end and until? Luka_Gaming07 534 — 5y

3 answers

Log in to vote
0
Answered by
Cizox 80
5 years ago
Edited 5 years ago

It's most likely due to the syntax of your script. A repeat loop requires a while (condition) at the end to specify what conditions are necessary for the repeat loop to continue, and once that condition returns false, then the loop ends. Secondly, if else statements require an end at the end of the block. Here is a working version of your script:

local player = game.Players.LocalPlayer.leaderstats.Level.Value

repeat 
    wait(10)
    if player == 1 then
        wait(0.1)
        script.Parent.Text = "Private I (Lvl 1)"
    elseif player == 2 then
        wait(0.1)
        script.Parent.Text = "Private II (Lvl 2)"
    elseif player == 3 then
        wait(0.1)
        script.Parent.Text = "Private III (Lvl 3)"
    end -- Added this 'end' keyword
while (CONDITION) -- Added this statement

I do not know what is your terminal condition is, which is why I wrote "CONDITION" where you should specify what you want it to be. If you are looking for this loop to continue infinitely, then I suggest replacing repeat with while true do and removing the last line. Furthermore, make sure that this is inside a LocalScript, and that the LocalScript is inside StarterGUI. Hope this helps.

Ad
Log in to vote
1
Answered by
KingDomas 153
5 years ago
Edited 5 years ago

There are 2 possible problems that I can see from reading this.

1)You haven't put it in a local script which will therefore make LocalPlayer not work. 2)You forgot to put the right amount of "end" on the end of the script.

Log in to vote
0
Answered by 5 years ago

This is because local player = game.Players.LocalPlayer.leaderstats.Level.Value is saved as a number, not a property, so to fix this, define it as local player = game.Players.LocalPlayer.leaderstats.Level and change player in the if conditions to player.Value.Also, apply WaitForChild on this because the instances (Level, leaderstats) might have not loaded at the time of defining.

0
You forgot the ``end`` at the end as well SuperSamyGamer 316 — 5y

Answer this question