Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
lo_1003 42
4 years ago
Edited 4 years ago
``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

Log in to vote
0
Answered by 4 years ago

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)
0
it works thx lo_1003 42 — 4y
Ad

Answer this question