Changing value when an object is free for 3 seconds, but I want the timer to reset when it is picked up before its value is changed

Hello. There’s a ball and when it is free, the hitbox of all players will get big for 3 seconds, however when it is picked up (even if its immediately after its free) then the hitboxes will return to their original size.

Here’s the code:

isFree.Changed:Connect(function()
	
	if isFree.Value == true then
		for i, v in pairs(game.Players:GetPlayers()) do
			
			local char = v.Character
			local cPart = char:FindFirstChild("ColliderPart")
			
			
			cPart.Size = Vector3.new(6,6,6)
			
			cooldown = task.spawn(function()
				
				task.delay(3, function()
					cPart.Size = Vector3.new(2,6,1)
				end)
				
			end)
			
		end
	

	elseif isFree.Value == false then
		
		for i, v in pairs(game.Players:GetPlayers()) do

			local char = v.Character
			local cPart = char:FindFirstChild("ColliderPart")

			if cPart.Size == Vector3.new(6,6,6) then
				cPart.Size = Vector3.new(2,6,1)
				
				task.cancel(cooldown)
				
			end


		end
	end
	
end)

My problem right now is that task.delay is stacking so I need to find a way to cancel the cooldown once the isFree value is false again. Currently I am not getting any errors it just doesn’t work, I guess I got task. stuff wrong. What is the best way to achieve this?

Hi ! You can maybe use like a repeat until loop and tick to substitute the task.delay function

ticks are basically the amount of time pass a certain date. so if you record the current tick, and wait 3 second and subtract it by the current tick, you should get close to 3 seconds

eg)

cooldown = tick()
repeat wait() until (tick-cooldown) >= 3

now, you need away of canceling it . why not make the same repeat loop have another condition for breaking?

eg)

cooldown = tick()
repeat wait() until (tick-cooldown) >= 3

if IsFree.Value == true then
--change hitbox size to small
end

hope this helps
good luck on your game :+1:

3 Likes

Nope this doesn’t work. I also tried similar things such as:

task.delay(3, function()
     if isFree.Value == true then

     end
end)

This requires the ball to not be free precisely at third second to cancel the countdown so it stacks anyways.

1 Like
local cooldown 
isFree.Changed:Connect(function()
	
	if isFree.Value == true then
		for i, v in pairs(game.Players:GetPlayers()) do
			
			local char = v.Character
			local cPart = char:FindFirstChild("ColliderPart")
			
			
			cPart.Size = Vector3.new(6,6,6)
			
			cooldown = task.spawn(function()
				
				local coolTick = tick()
				repeat wait() until (tick()-coolTick) >= 3

				if isFree.Value == true then
					cPart.Size = Vector3.new(2,6,1)
				end
				
			end)
			
		end
	

	elseif isFree.Value == false then
		
		for i, v in pairs(game.Players:GetPlayers()) do

			local char = v.Character
			local cPart = char:FindFirstChild("ColliderPart")

			if cPart.Size == Vector3.new(6,6,6) then
				cPart.Size = Vector3.new(2,6,1)
				
				task.cancel(cooldown)
				print("cancelled?")
				
			end


		end
	end
	
end)

Nevermind I got what you said wrong my bad. It works now, thank you.

2 Likes

you should make it into

			cooldown = task.spawn(function()
				
				local coolTick = tick()
				repeat wait() until (tick()-coolTick) >= 3 or isFree.Value == false

				if isFree.Value == true then
					cPart.Size = Vector3.new(2,6,1)
				end
				
			end)

that will break the loop if IsFree is change to false which will further prevent error and maybe even remove the need for task.cancel()

1 Like

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