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

How can I make the gun semi-automatic?

Asked by
Mr_Unlucky 1085 Moderation Voter
5 years ago

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)
0
wait(2) GIassWindows 141 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Add a debounce.

local canShoot = true

tool.Activated:Connect(function()
    if not canShoot then
        canShoot = false
        fire()
        wait(2)
        canShoot = true
    end
end)
0
the gun no longer shoots now Mr_Unlucky 1085 — 5y
0
Let me edit it. User#19524 175 — 5y
0
still rapid fires. perhaps it's because of the if bullets <= 0 statements. Mr_Unlucky 1085 — 5y
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. User#19524 175 — 5y
View all comments (3 more)
0
it's still not working. Mr_Unlucky 1085 — 5y
0
Edited. Try it now. User#19524 175 — 5y
0
Edited. Try it now. User#19524 175 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

i need help!~~~~~~~~~~~~~~~~~ help me! ~~~~~~~~~~~~~~~~~

0
yes i do nned help Mr_Unlucky 1085 — 5y

Answer this question