If-Statement trouble

I’m making an overhead rank system for my game but am running into an issue. The player’s rank is repeatedly set the the first rank, “Scout”, even if they have enough wins for other ranks.

wait(5)

local ServerStorage = game:GetService("ServerStorage")

local player = game.Players:FindFirstChild(script.Parent.Name)
local wins = player:WaitForChild("leaderstats"):WaitForChild("Wins").Value
local gui = player.Character:WaitForChild("Head"):WaitForChild("RankGui").TextLabel

local rankGui = ServerStorage:WaitForChild("RankGui")

local Scout = Color3.new(0.666667, 0.596078, 0.607843)
local Pioneer = Color3.new(0.298039, 0.498039, 0.419608)
local Roamer = Color3.new(1, 0.333333, 0)
local Wanderer = Color3.new(0.333333, 0.333333, 0)
local Pathfinder = Color3.new(0, 0.333333, 0)
local Explorer = Color3.new(1, 1, 0)
local Seeker = Color3.new(1, 0, 1)
local Adventurer = Color3.new(0, 0, 1)
local Vanguard = Color3.new(0, 0.333333, 1)
local Expeditionist = Color3.new(1, 0, 0)
local Apex = Color3.new(1, 1, 0.498039)


local function updateRank()
	print("Updating player rank..")

	if wins < 1 then
		gui.Text = "Scout"
		gui.TextColor3 = Scout
	elseif wins < 5 then
		gui.Text = "Pioneer"
		gui.TextColor3 = Pioneer
	elseif wins < 10 then
		gui.Text = "Roamer"
		gui.TextColor3 = Roamer
	elseif wins < 15 then
		gui.Text = "Wanderer"
		gui.TextColor3 = Wanderer
	elseif wins < 20 then
		gui.Text = "Pathfinder"
		gui.TextColor3 = Pathfinder
	elseif wins < 25 then
		gui.Text = "Explorer"
		gui.TextColor3 = Explorer
	elseif wins < 30 then
		gui.Text = "Seeker"
		gui.TextColor3 = Seeker
	elseif wins < 35 then
		gui.Text = "Adventurer"
		gui.TextColor3 = Adventurer
	elseif wins < 40 then
		gui.Text = "Vanguard"
		gui.TextColor3 = Vanguard
	elseif wins < 45 then
		gui.Text = "Expeditionist"
		gui.TextColor3 = Expeditionist
	else
		gui.Text = "Apex"
		gui.TextColor3 = Apex
	end
end

wait(2)

updateRank()


player:WaitForChild("leaderstats"):WaitForChild("Wins").Changed:Connect(function()
	updateRank()
end)



2 Likes

Show us the whole script. What’s wins? You might be checking before it’s updated.

2 Likes

You haven’t defined wins yet, so it defaults to nil and nil < 1.

1 Like

Well no not really. It would give an error of something like: attempt to compare nil with number

Although we have no idea what wins is set to. It could be a leaderstats value or just something else. But this seems to be a LocalScript so I doubt that

1 Like

Well then, I have no idea.

2 Likes

I edited my post, the full script is now there.

Why are you waiting for Wins twice? That doesn’t make any sense.

1 Like

Are you changing the leaderstats wins on the server (“normal script”) or on the client (LocalScript)?

1 Like

The wins leaderstat is being increased by a proximityprompt inside of a part via Server script.

1 Like

Are you sure whatever is updating Wins is on the server-side? If it’s being set by another client then it might not work (I don’t really know how leadrerstats work, so excuse me if I am incorrect)

1 Like

The script I showed is a ServerScript inside of StarterCharacterScripts

1 Like

If I am guessing, you already defined the wins variables value meaning you only have a static non changing number (if I can explain that well).
Remove .Value and on obtain it when you call the function.
Also for good practices, pass in the wins argument.

wait(5)

local ServerStorage = game:GetService("ServerStorage")

local player = game.Players:FindFirstChild(script.Parent.Name)
local win = player:WaitForChild("leaderstats"):WaitForChild("Wins") -- .Value removed
local gui = player.Character:WaitForChild("Head"):WaitForChild("RankGui").TextLabel

local rankGui = ServerStorage:WaitForChild("RankGui")

local Scout = Color3.new(0.666667, 0.596078, 0.607843)
local Pioneer = Color3.new(0.298039, 0.498039, 0.419608)
local Roamer = Color3.new(1, 0.333333, 0)
local Wanderer = Color3.new(0.333333, 0.333333, 0)
local Pathfinder = Color3.new(0, 0.333333, 0)
local Explorer = Color3.new(1, 1, 0)
local Seeker = Color3.new(1, 0, 1)
local Adventurer = Color3.new(0, 0, 1)
local Vanguard = Color3.new(0, 0.333333, 1)
local Expeditionist = Color3.new(1, 0, 0)
local Apex = Color3.new(1, 1, 0.498039)


local function updateRank(wins) -- wins argument added
	print("Updating player rank..")

	if wins < 1 then
		gui.Text = "Scout"
		gui.TextColor3 = Scout
	elseif wins < 5 then
		gui.Text = "Pioneer"
		gui.TextColor3 = Pioneer
	elseif wins < 10 then
		gui.Text = "Roamer"
		gui.TextColor3 = Roamer
	elseif wins < 15 then
		gui.Text = "Wanderer"
		gui.TextColor3 = Wanderer
	elseif wins < 20 then
		gui.Text = "Pathfinder"
		gui.TextColor3 = Pathfinder
	elseif wins < 25 then
		gui.Text = "Explorer"
		gui.TextColor3 = Explorer
	elseif wins < 30 then
		gui.Text = "Seeker"
		gui.TextColor3 = Seeker
	elseif wins < 35 then
		gui.Text = "Adventurer"
		gui.TextColor3 = Adventurer
	elseif wins < 40 then
		gui.Text = "Vanguard"
		gui.TextColor3 = Vanguard
	elseif wins < 45 then
		gui.Text = "Expeditionist"
		gui.TextColor3 = Expeditionist
	else
		gui.Text = "Apex"
		gui.TextColor3 = Apex
	end
end

wait(2)

updateRank(win.Value)


player:WaitForChild("leaderstats"):WaitForChild("Wins").Changed:Connect(function()
	updateRank(win.Value)
end)
2 Likes

You could also listen to when its value changes like so:

player:WaitForChild(“leaderstats”):WaitForChild(“Wins”):GetPropertyChangedSignal(“Value”):Connect(function()
updateRank()
end)

This way it only fires when the Value property of Wins changes

1 Like

This isn’t good practice. I’m not sure why you’re doing this.

1 Like

I will try that.

How else do I get the player though?

I mean yeah it’s not standard or ideal but it definitely does work. And we just found out that it’s a server script so

1 Like

You should probably remove the .Value when defining the wins variable, like ado said. That will fix your problem.

1 Like

You would use:

game.Players.PlayerAdded:Connect(function(Player)

end)
1 Like

Players.LocalPlayer if it’s a local script

1 Like

Well why do you have to get the player in that manner? Why name a script after a player?

1 Like