Function is not firing with no errors

I have a simple function that adds 1 exp point to a player every 2 seconds. It’s just a regular script located in the ServerScriptService. The function is not firing, but there are no error messages.

I tried locating the line of code in a separate script and even placed it in the StarterPlayerScripts. This is my first time creating a level system so I don’t entirely understand what’s going wrong here, it’s probably something obvious.

game.Players.PlayerAdded:Connect(function(player)
	local level = Instance.new("IntValue", player)
	level.Name = "Level"
	level.Value = 1
	
	local exp = Instance.new("IntValue", level)
	exp.Name = "Current"
	exp.Value = 0
	
	local maxExp = Instance.new("IntValue", level)
	maxExp.Name = "Max"
	maxExp.Value = 100
	
	
	exp.Changed:Connect(function(val)
		if exp.Value >= maxExp.Value then
			-- Do stuff.
			
			level.Value = level.Value + 1
			exp.Value = 0
			maxExp.Value = maxExp.Value * 1.25
			
			function additionXP()
				player.Level.Current.Value = player.Level.Current.Value + 1
				wait(2)
			end
			
			while true do
				additionXP()
			end
			
		end
	end)
end)

By the looks of your code the loop doesn’t start until something changes any value of the instance exp. Is that perhaps the issue?

1 Like

So I need to set a trigger for it to start when a player joins?

Your exp.Changed function is firing but because exp.value is NOT greater than or equal to the maxExp.Value, the code below the if statement never runs.

Edit: I do not actually know if it is firing though because exp.value has to change in order for the code beneath to run.

1 Like

Yes! Silly me, completely disregarded that to be a possible issue.

No problem :slight_smile: . I just want you to know though that the code will not run underneath the event unless the exp.Value changes.