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 6 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.

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

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


1 answer

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

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

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

A perfectly fine loop, except for two things

script.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.

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

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

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

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 — 6y
1
You are actually the best, it worked, thank you so much! also the text is displayed on the players exp bar. 1freshkidz1 12 — 6y
Ad

Answer this question