Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I create a cool down time before they can fire again?

I'm trying to make a system which allows a player to shoot a spell, but, I don't want them to be able to continuously use the spell without a cool down. If there is no cool down, the player has the ability to use an auto clicker which spams the spell and creates a load of lag within game, hence why I would like to make one.

However, when I tried creating a cool down, I used a debounce but I kept getting errors and things kept going underlined then other functions of my script wouldn't work.. The script below is the fire script for one of my spells WITHOUT the cool down as mine didn't seem to work.. Please could someone help.

I would like the cool down to be a 0.5 second wait per click..

game.ReplicatedStorage.Powers.OnServerEvent:Connect(function(player, MouseHit, Handle)

if player.Spell.Value == "Water" then

  local S1 = Instance.new("Part",workspace)

  local S1b = Instance.new("BodyVelocity")

  S1.CFrame = Handle.CFrame + Handle.CFrame.lookVector*3

  S1.Anchored = false

  S1.CanCollide = true

  S1.formFactor = "Symmetric"

  S1.Shape = "Ball"

  S1.Size = Vector3.new(1,1,1)

  S1.TopSurface = "Smooth"

  S1.BottomSurface = "Smooth"

  S1.Transparency = 0.5

  S1.Elasticity = 1

  S1.Friction = 0

  S1.BrickColor = BrickColor.new("Bright blue")

  S1b.Velocity = MouseHit.lookVector*100

  S1b.Parent = S1

  game:GetService("Debris"):AddItem(S1, 2)

  local part = S1

  part.Touched:connect(function(hit)

  if hit.Parent then

    local hum = hit.Parent:FindFirstChild("Humanoid")

    if hum then

      if hum.Parent.Name == player.Name then

        wait(0.01)

      else

        hum:TakeDamage(2)

        S1:Destroy()


      end

    end

  end

  end)

end

end)

Thank you.

2 comments

0

Use debounce bro. wiki.roblox.com/index.php?title=Debounce

0

" I used a debounce but I kept getting errors and things kept going underlined then other functions of my script wouldn't work"

1 answer

✓ Accepted answer

You're right you would use a debounce. How ever you done it was probably wrong because there's no reason why logically it would break a script.

db = false
game.ReplicatedStorage.Powers.OnServerEvent:Connect(function(player, MouseHit, Handle)
if not db then
	db = true
	if player.Spell.Value == "Water" then
	
	  local S1 = Instance.new("Part",workspace)
	
	  local S1b = Instance.new("BodyVelocity")
	
	  S1.CFrame = Handle.CFrame + Handle.CFrame.lookVector*3
	
	  S1.Anchored = false
	
	  S1.CanCollide = true
	
	  S1.formFactor = "Symmetric"
	
	  S1.Shape = "Ball"
	
	  S1.Size = Vector3.new(1,1,1)
	
	  S1.TopSurface = "Smooth"
	
	  S1.BottomSurface = "Smooth"
	
	  S1.Transparency = 0.5
	
	  S1.Elasticity = 1
	
	  S1.Friction = 0
	
	  S1.BrickColor = BrickColor.new("Bright blue")
	
	  S1b.Velocity = MouseHit.lookVector*100
	
	  S1b.Parent = S1
	
	  game:GetService("Debris"):AddItem(S1, 2)
	
	  local part = S1
	
	  part.Touched:connect(function(hit)
	
	  if hit.Parent then
	
	    local hum = hit.Parent:FindFirstChild("Humanoid")
	
	    if hum then
	
	      if hum.Parent.Name == player.Name then
	
	        wait(0.01)
	
	      else
	
	        hum:TakeDamage(2)
	
	        S1:Destroy()
	
	
	      end
	
	    end
	
	  end
	
	  end)
	
	end
	wait(0.5) -- Specified 0.5 second delay between firing
	db = false
  end
end)

Please don't post scripts with whitespace between every single line, it's so annoying

This discussion is preserved as it was. New answers, comments, and votes are closed.