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

Why is this gun only reloading once and also showing that you have 0 ammo in the name?

Asked by
Jephi 42
7 years ago

Gun script:

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local ammo = 8
local isReload = false
local mouse = game.Players.LocalPlayer:GetMouse()

function reload(amount)
    tool.Name = "Reloading"
    isReload = true
    wait(2)
    tool.Name = "Pistol" .. tostring(ammo)
end


tool.Equipped:connect(function(mouse)
    print("Pistol Equipped")
    game:GetService('UserInputService').InputBegan:connect(function(input, gameprocessed)
        if input.KeyCode == Enum.KeyCode.R then

            reload(8)
        end
    end)
    mouse.Button1Down:connect(function()
    print("Mouse Clicked")
    if (not isReload) and ammo > 0 then --if Player is not reloading
            ammo = ammo - 1
            tool.Name = "Pistol " ..tostring(ammo)  
            local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 200)
            local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

            local beam = Instance.new("Part", workspace)
            beam.BrickColor = BrickColor.new("Gray")
            beam.FormFactor = "Custom"
            beam.Material = "Neon"
            beam.Transparency = 0.75
            beam.Anchored = true
            beam.Locked = true
            beam.CanCollide = false

            local light = Instance.new("PointLight", beam)
            light.Range = 15

            local distance = (tool.Handle.CFrame.p - position).magnitude
            beam.Size = Vector3.new(0.3, 0.3, distance)
            beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, - distance / 2)

            game:GetService("Debris"):AddItem(beam, 0.1)

            if part then
                local humanoid = part.Parent:FindFirstChild("Humanoid")

                if not humanoid then
                    humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
                end

                if humanoid then
                    humanoid:TakeDamage(30)
                end
             end
        else
            reload(8)
        end
    end)
end)


The problem lies with the name of the pistol going down to 0 ammo, and it only works once. After that it keeps reloading once shot and ammo remains at 0.

0
You do not set ammo back to 8 in the reload function User#5423 17 — 7y

Answer this question