Script adds to value twice

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

    As the title suggests the script keeps adding twice to a value.

  2. What is the issue? Include screenshots / videos if possible!
    https://streamable.com/dc2xv6

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    tried adding debounce did nothing…
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

while wait() do
	valuesFolder.XpDifference.Value = valuesFolder.XpNeeded.Value - valuesFolder.CurrentXp.Value
	if valuesFolder.CurrentLevel.Value < 1 then
		valuesFolder.XpNeeded.Value = 100
	else
		valuesFolder.XpNeeded.Value = math.floor(valuesFolder.CurrentLevel.Value ^ 1.3) * 200 + math.floor((valuesFolder.CurrentLevel.Value - 1) ^ 4)
	end

	if valuesFolder.XpDifference.Value <= 1 then
		valuesFolder.CurrentLevel.Value = valuesFolder.CurrentLevel.Value + 1
	end 
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

It’s because you calculate the XpDifference before you change the Xpneeded. Try this:

while wait() do
	if valuesFolder.CurrentLevel.Value < 1 then
		valuesFolder.XpNeeded.Value = 100
	else
		valuesFolder.XpNeeded.Value = math.floor(valuesFolder.CurrentLevel.Value ^ 1.3) * 200 + math.floor((valuesFolder.CurrentLevel.Value - 1) ^ 4)
	end
    valuesFolder.XpDifference.Value = valuesFolder.XpNeeded.Value - valuesFolder.CurrentXp.Value
	if valuesFolder.XpDifference.Value <= 1 then
		valuesFolder.CurrentLevel.Value = valuesFolder.CurrentLevel.Value + 1
	end 
end