Variables Not Changing

I’m trying to set a variable in a function and then use it later in my script to change a nametag to display their rank, but when I set the value in the script it still shows up as nil when I try to print it. When the character is reset the rankName changes, but it’s color doesn’t.

Here’s what’s happening, and the code I’m using.

Code:

print(wins)
if wins < 5 then

        rankName = "Newbie"

        rankColor = Color3.new(42, 182, 131)

    end

Output:

3
nil
nil

Expected output:

3
Newbie
Color3 Value

Thanks!

Did you declare the rankName and rankColor variables outside of the if statement?

print(wins)

local rankName
local rankColor
if wins < 5 then
	rankName = "Newbie"
	rankColor = Color3.fromRGB(42, 182, 131)
end

You can read more about Variable Scopes under Globals and Locals in the Roblox API: Variables | Roblox Creator Documentation

Yes, at the top of the script.


local rankName;

local rankColor;

I’m trying to run the function if the value of wins is below 5, am I doing it incorrectly?

No, it seems like some other parts of your script are not working or something. Maybe your variables are getting overwritten or something. I don’t know.

It could also be possible that you are changing the variable in a LocalScript which is then not replicated to the Server if it checks the variable.

Yup, definitely in another spot. I was referencing the head incorrectly. Thanks for the help!