I'm making a sniper rifle, and I want to make the gun semi-automatic. The problem is, is that whenever I'm attempting to do it, you can spam click to shoot faster. How can I make it so you need to wait two seconds before shooting?
tool = script.Parent anim = tool.Animation attacking = false local animation = nil local tool = script.Parent local player = game:GetService("Players").LocalPlayer mousedown = false mouse = game.Players.LocalPlayer:GetMouse() local HUD = tool.HUD bullets = 1 reloading = false tool.Equipped:Connect(function(mouse) animation = tool.Parent.Humanoid:LoadAnimation(anim) animation:Play() local newHUD = HUD:Clone() newHUD.Parent = player.PlayerGui newHUD.WepName.Text = tool.Name newHUD.WepName.Bullets.Text = bullets end) tool.Activated:Connect(function() end) tool.Deactivated:Connect(function() mousedown = false end) function fire() if bullets <= 0 then reloading = true tool.Reload:Play() wait(0.5) bullets = 1 player.PlayerGui.HUD.WepName.Bullets.Text = bullets reloading = false else local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300) local touch, position = workspace:FindPartOnRay(ray, game.Players.LocalPlayer.Character, false, true) local trace = Instance.new("Part") trace.Anchored = trace trace.CanCollide = false trace.Transparency = 0.5 trace.BrickColor = BrickColor.new("Black") trace.Material = Enum.Material.SmoothPlastic local distance = (tool.Handle.CFrame.p - position).magnitude trace.Size = Vector3.new(0, 0, distance) trace.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2) trace.Parent = workspace bullets = bullets - 1 player.PlayerGui.HUD.WepName.Bullets.Text = bullets tool.Fire:Play() wait(0.3) trace:Destroy() if touch then local humanoid = touch.Parent:FindFirstChild("Humanoid") if not humanoid then humanoid = touch.Parent.Parent:FindFirstChild("Humanoid") end if humanoid then humanoid:TakeDamage(80) end end end end tool.Unequipped:Connect(function() player.PlayerGui.HUD:Destroy() animation:Stop() end)
Add a debounce.
local canShoot = true tool.Activated:Connect(function() if not canShoot then canShoot = false fire() wait(2) canShoot = true end end)
i need help!~~~~~~~~~~~~~~~~~ help me! ~~~~~~~~~~~~~~~~~