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

Whats Wrong with my if then Statement?

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

✓ Accepted answer

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.

2 comments

0

It’s actually Color3.fromRGB that needs to be used and not Color3.new. You should edit your answer. :P

1

You are actually the best, it worked, thank you so much! also the text is displayed on the players exp bar.

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