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

Whats Wrong with my if then Statement?

Asked by 7 years ago

Whats Supposed to happen is if the player reaches level 5 then the color of the text will change, but I don't know what I'm doing wrong.

1while wait(1) do
2    script.Parent.Text = ""..game.Players.LocalPlayer.Data.Level.Value
3end
4 
5local level = script.Parent
6if level.Text = "5" then
7level.TextColor3 = (255, 187, 0)
8end

1 answer

Log in to vote
4
Answered by
theCJarmy7 1293 Moderation Voter
7 years ago
Edited 7 years ago

Okay, let me take you through your own mistakes one step at a time.

1while wait(1) do
2    script.Parent.Text = ""..game.Players.LocalPlayer.Data.Level.Value
3end

A perfectly fine loop, except for two things

1script.Parent.Text = ""..game.Players.LocalPlayer.Data.Level.Value

that "".. is entirely useless. I'm not sure what you were trying to do there, but if you were trying to turn .Value into a string, try tostring(). Secondly, your loop runs forever. The other parts of your code will never execute because your loop just never ends.

1local level = script.Parent
2if level.Text = "5" then
3level.TextColor3 = (255, 187, 0)
4end

the == operator will check if something is the same as another thing, the equals sign will set a value to something, you actually use it for just that purpose immediately above and below the flawed if statement. As for the color changing part, you need to use Color3.new(255,187,0) to actually change the color, leaving it as is will throw an error, it will also tell you something is wrong just in the text editor. Some fixed code would look something like this

1while wait(1) do --you might want to use spawn() with this if you have more code
2    script.Parent.Text = tostring(game.Players.LocalPlayer.Data.Level.Value)
3-- assumes such an object exists
4    local level = script.Parent
5    if level.Text == "5" then
6        level.TextColor3 = Color3.new(255,187,0)
7    end
8end

However, I would recommend not relying on the text to determine a player's level, it just seems wrong to me.

0
It’s actually Color3.fromRGB that needs to be used and not Color3.new. You should edit your answer. :P User#20279 0 — 7y
1
You are actually the best, it worked, thank you so much! also the text is displayed on the players exp bar. 1freshkidz1 12 — 7y
Ad

Answer this question