Script not working properly. | Help is needed!

Basically, I have made a script and wrapped it in a coroutine. It checks for the player’s game time in the game and if it is certain, it will change their in game leaderstats. When I increase it, it just stays on Beginner.

Note that this is the half script ( the other half is data stores)

--// Variables, Services and functions

local dataStore = game:GetService("DataStoreService"):GetDataStore("TimeDataStore")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local gameSeconds = ReplicatedStorage:WaitForChild("GameSeconds")

local playersLeft = 0

local defaultTime  = 0

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	
	     playersLeft = playersLeft + 1
	
	     local leaderstats = Instance.new("Folder")
	     leaderstats.Name = "leaderstats"
	     leaderstats.Parent = player    
	
	     local playerName = Instance.new("StringValue")
	     playerName.Name = "Name"
	     playerName.Value =  player.Name
	     playerName.Parent = leaderstats

	     local Teir = Instance.new("StringValue")
	     Teir.Name = "Teir"
	     Teir.Value = "None"
	     Teir.Parent = leaderstats
	
         local timeSpent = Instance.new("IntValue")
	     timeSpent.Name = "Game Time"
	     timeSpent.Value = gameSeconds.Value
	     timeSpent.Parent = leaderstats
	
	     local newThread = coroutine.create(function()
	           while true do
		        player.leaderstats["Game Time"].Value = player.leaderstats["Game Time"].Value  + 1
		        if player.leaderstats["Game Time"].Value >= 7199 then
			         player.leaderstats.Teir.Value = "Beginner"
			   elseif player.leaderstats["Game Time"].Value >= 17999 then
				      player.leaderstats.Teir.Value = "Novice"
				elseif player.leaderstats["Game Time"].Value >= 53999 then
					  player.leaderstats.Teir.Value = "Expert"
			   elseif player.leaderstats["Game Time"].Value >= 107999 then
					   player.leaderstats.Teir.Value =  "Overseer"
		                end
		        wait(1)
		       end
	end)
	   coroutine.resume(newThread)

Also, I change it on the server since changing it on the client won’t replicate. The other solution I was thinking was to arrange these statements from high to low.

The problem is that your conditions are overlapping and the larger condition is evaluated first. For example

local number = 5

if number > 3 then
    print("First condition!")
elseif number > 4 then
    print("Second condition!")
end

Since we’re using an elseif, the second condition is only evaluated if the first is false, if the first is true then no other conditions are evaluated.

Applying this to your case, each tier above beginner is caught by the first condition of beginner, since they are all checking for some time >= 7199. To fix this you should either order your conditions opposite (highest number first) or add limits on both sides of the number (beginner would be time >= 7199 and time < 17999)

1 Like

Thanks! Done that. ( 30 characters)