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

how can i put a cooldown in this script for a gun?

``function GetBullet()
 local p = Instance.new("Part")
 p.Shape = Enum.PartType.Ball
 p.Parent = workspace
 p.Size = Vector3.new(.5, .1, .5)
 p.BrickColor = BrickColor.Black()
 p.CanCollide = false
 Instance.new("BodyVelocity", p)
 return p
end

script.Parent.Equipped:Connect(function(mouse)
 mouse.Button1Down:Connect(function()
  local mH = mouse.Hit
  local bullet = GetBullet()
  bullet.CFrame = script.Parent.Handle.CFrame * CFrame.new(0, 0, -1.5)
  bullet.CFrame = CFrame.new(bullet.Position, mH.p)
  bullet.BodyVelocity.Velocity = bullet.CFrame.LookVector * 120
  bullet.Touched:Connect(function(hit)
   local Humanoid = hit.Parent:FindFirstChild("Humanoid")
   if Humanoid then
    Humanoid:TakeDamage(15)
    bullet:Destroy()
   end
  end)
 end)
end)

help me put a cooldown on this script

1 answer

✓ Accepted answer

Add debounce and a wait, Like this. debounce makes so the function wont run multipile times.

function GetBullet()
 local p = Instance.new("Part")
 p.Shape = Enum.PartType.Ball
 p.Parent = workspace
 p.Size = Vector3.new(.5, .1, .5)
 p.BrickColor = BrickColor.Black()
 p.CanCollide = false
 Instance.new("BodyVelocity", p)
 return p
end

local debounce = true

script.Parent.Equipped:Connect(function(mouse)
 mouse.Button1Down:Connect(function()
if debounce then
debounce = false
  local mH = mouse.Hit
  local bullet = GetBullet()
  bullet.CFrame = script.Parent.Handle.CFrame * CFrame.new(0, 0, -1.5)
  bullet.CFrame = CFrame.new(bullet.Position, mH.p)
  bullet.BodyVelocity.Velocity = bullet.CFrame.LookVector * 120
  bullet.Touched:Connect(function(hit)
   local Humanoid = hit.Parent:FindFirstChild("Humanoid")
   if Humanoid then
    Humanoid:TakeDamage(15)
    bullet:Destroy()
   end
  end)
wait(0.75)   -- or how much u want it to wait
debounce = true
end
 end)
end)

1 comment

0

it works thx

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