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

How can I make the gun semi-automatic?

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)

1 comment

0

wait(2)

2 answers

Add a debounce.

local canShoot = true

tool.Activated:Connect(function()
    if not canShoot then
        canShoot = false
        fire()
        wait(2)
        canShoot = true
    end
end)

7 comments

0

the gun no longer shoots now

0

Let me edit it.

0

still rapid fires. perhaps it's because of the if bullets <= 0 statements.

0

if bullets == 0. Change it to that. The < means it can go under 0. I know the = saves the if statement, but the < is still allowing it to go under 0.

0

it's still not working.

0

Edited. Try it now.

0

Edited. Try it now.

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