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

Why won't the reload system work?

Asked by
Xyternal 247 Moderation Voter
2 years ago

So i am trying to make a reload system for a gun im making, but when ever i reach 30, the mag falls out, but a new one doesn't spawn back in. Why? Here is the script.

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local sound = script.Parent.Handle.shot
local shooting = false
local ammo = 30
local equipped = false
local mouseConnection
tool.Equipped:Connect(function(mouse)
equipped = true
    mouseConnection = mouse.Button1Down:Connect(function()
        shooting = true
        while shooting and equipped do
            ammo = ammo - 1
            print(ammo)
            if ammo < 1 then
                for i, v in pairs(script.Parent.Muzzel:GetChildren()) do
                    v.Destroy()
                end
                tool.Muzzel:Clone()
                local Muzzel = tool.Muzzel:Clone()
                Muzzel.Parent = tool
                Muzzel.Name = "Muzzel"
                Muzzel.Position = game.Muzzel.Position
            end
        print("Mouse pressed!")
        sound:Play()
        local ray = Ray.new(tool.Muzzel.CFrame.p, (mouse.Hit.p - tool.Muzzel.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)



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







        local distance = (tool.Muzzel.CFrame.p - position).magnitude
        beam.Size = Vector3.new(0.3, 0.3, distance)
        beam.CFrame = CFrame.new(tool.Muzzel.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
            mouse.Button1Up:Connect(function()

                shooting = false

            end)
            wait(0.1)
            end
    end)
end)

tool.Unequipped:Connect(function()

    equipped = false

    mouseConnection:Disconnect()

end)


Answer this question