Help with tower attack cooldown

Basically, I’m making a tower defense game, and I have this 1 tower with a massive cooldown (50 seconds.) problem is that when something enters the range, it must stay in the tower’s range for 50 seconds. I want the cooldown to keep counting down even while there are no enemies in the range.

Here is my script:

image

Not sure how to make this work. Everything I tried results in a timeout.

2 Likes

Probably coroutine and task.spawn(function()end) is your fiend here.
Other option is of course to create numberValue (Instance.new("NumberValue")) and create coroutine/task.spawn() loop, which will count and down and set value as going down. In the checking function above you can check if NumberValue.Value > 0 and if true, return. Else continue the function.

if config.Parent and not NumberValue.Value > 0 then
  if config:FindFirstChild("WaitBefore") then
    ...
  end
end

something like that. I just saw that you are already using NumberValue.Value in here.

1 Like

An alternative would be using the service Debris, especially the AddItem(instance, lifetime) method. You would create a random instance (preferrably one with low data like stringValue). You would name it “Cooldown”, parent it to your tower, and set its lifetime to your tower’s cooldown (50 seconds). When you make your loops, keep your checks, but right before deciding to attack, check if your tower has a child named “Cooldown” (like this : tower:FindFirstChild("Cooldown") and if it returns true, then your troop can’t attack because the cooldown is still active).

I think that the Debris service is really reliable on the lifetime so this might be a better solution to the problem.

1 Like