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
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.
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()