How to fix tracecondition?

Hi, so long i did not ask something on this forum,
i making popup that show when value changed, but i got some issue, the trace condition, here is my script

local rootFrame = script.Parent
local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local leaderstats_solved : IntValue = leaderstats:WaitForChild("Solved")
local mainText = rootFrame.show
local lastValue = 0
local replicatedStorage = game:GetService("ReplicatedStorage")
local popup_gui = replicatedStorage.assets.gui.solved
local popup_sound = replicatedStorage.assets.sound.popup
local cache = player.PlayerGui.cache
local waitTime = 1
local soundService = game:GetService("SoundService")

function setText()
	
end

function showPopup()
	local popup = popup_gui:Clone()
	popup.Position = UDim2.new(math.random(40, 70) / 100, 0, math.random(40, 70) / 100, 0)
	popup.Parent = cache
	soundService:PlayLocalSound(popup_sound)
	task.wait(1)
	popup:TweenPosition(rootFrame.Position, Enum.EasingDirection.In, Enum.EasingStyle.Quad, waitTime)
	task.wait(waitTime)
	popup:Destroy()
	mainText.Text = tonumber(mainText.Text) + ((leaderstats_solved.Value > lastValue) and 1 or -1)
	lastValue += ((leaderstats_solved.Value > lastValue) and 1 or -1)
end

function update(newValue : number, isAnimate : boolean)
	if isAnimate then
		for i = 1, math.abs(newValue - lastValue) do
			task.spawn(showPopup)
			task.wait(0.1)
		end
	else
		mainText.Text = newValue
		lastValue = newValue
	end
end

leaderstats_solved.Changed:Connect(function(newValue)
	update(newValue, true)
end)

update(leaderstats_solved.Value, false)

how to produce the trace condition?
simply, i must change the value and change again while first changing not done yet, like, changing value to 100, then before its done, change it to 0, its will be something weird, like, being -1, and its calculating like -1 > 0 > -1 > 0, when i ask ai,

i want to get how to force stop a task.spawn. or another alternative maybe help?
any suggestion are very appreciated!

UPDATE:
i found how to fix it out by simply every “for” loop i put it into task.spawn, then, i put the task.spawn on a variable, every function started and isAnimate = true, then the script gonna check is the cache are nil or not, if not nil, it will stop the task.spawn in cache then make new for that put on cache too, here the script:

local rootFrame = script.Parent
local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local leaderstats_solved : IntValue = leaderstats:WaitForChild("Solved")
local mainText = rootFrame.show
local lastValue = 0
local replicatedStorage = game:GetService("ReplicatedStorage")
local popup_gui = replicatedStorage.assets.gui.solved
local popup_sound = replicatedStorage.assets.sound.popup
local cache = player.PlayerGui.cache
local taskCache
local waitTime = 1
local soundService = game:GetService("SoundService")

function setText()
	
end

function showPopup()
	local popup = popup_gui:Clone()
	popup.Position = UDim2.new(math.random(40, 70) / 100, 0, math.random(40, 70) / 100, 0)
	popup.Parent = cache
	soundService:PlayLocalSound(popup_sound)
	task.wait(1)
	popup:TweenPosition(rootFrame.Position, Enum.EasingDirection.In, Enum.EasingStyle.Quad, waitTime)
	task.wait(waitTime)
	popup:Destroy()
	mainText.Text = tonumber(mainText.Text) + ((leaderstats_solved.Value > lastValue) and 1 or -1)
	lastValue += ((leaderstats_solved.Value > lastValue) and 1 or -1)
end

function update(newValue : number, isAnimate : boolean)
	if isAnimate then
		if taskCache then
			task.cancel(taskCache)
		end
		taskCache = task.spawn(function()
			for i = 1, math.abs(newValue - lastValue) do
				task.spawn(showPopup)
				task.wait(0.1)
			end
		end)
	else
		mainText.Text = newValue
		lastValue = newValue
	end
	print(taskCache)
end

leaderstats_solved.Changed:Connect(function(newValue)
	update(newValue, true)
end)

update(leaderstats_solved.Value, false)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.