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

Gun breaks while reloading, and pressing MouseButton 1? (v2)

Asked by 4 years ago

I wasn't clear enough last time, so i decided to re-post this since I haven't solved the issue.

The gun was created by me, I had followed the Roblox Wiki on creating a standard laser rifle, But added a ammo and reloading function. When you reload the function reloading will be set to true inside the script. But when you reload the gun while clicking Mouse Button 1 or LMB it wont fire again after the gun breaks. There's no error in console or anything so im just thinking it might be the reason because I identified reloading = true in the script instead of using a bool value or what.

Here's my code:

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local debounce = false
local maxammo = script.Parent:WaitForChild("maxammo")
local clipammo = script.Parent:WaitForChild("clipammo")
local reloading = false

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.R and reloading == false then
        print("reloading")
        reloading = true
        player.PlayerGui.Bullets.TextLabel.Text = "Reloading..."
        maxammo.Value = (maxammo.Value - (9 - clipammo.Value))
        clipammo.Value = 9
        wait(3)
        player.PlayerGui.Bullets.TextLabel.Text = clipammo.Value.." / "..maxammo.Value
        reloading = false
        print("reloading finished")
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

tool.Equipped:connect(function(mouse)
    local BulletsGui = script.Parent.Bullets

        BulletsGui:Clone().Parent = player.PlayerGui
        BulletsGui.TextLabel.Text = clipammo.Value.." / "..maxammo.Value
    mouse.Button1Down:connect(function()

    if not debounce then


        debounce = true
if clipammo.Value <= 0 or maxammo.Value <= 0 then end
    if reloading == true then end
    if clipammo.Value > 0 and reloading == false then

        clipammo.Value = clipammo.Value - 1
        player.PlayerGui.Bullets.TextLabel.Text = clipammo.Value.." / "..maxammo.Value

        local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

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

        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(math.random(1, 3))
            end
        end

        wait(0.39)

        debounce = false
    end

tool.Unequipped:Connect(function()
    player.PlayerGui.Bullets:Destroy()
end)
    end
    end)
end)

0
The program relies on declaring the Bullets, however, upon unequipping, it is permanently removed, it is advised you set the parent to nil instead, and redefine it in the Equip function Ziffixture 6913 — 4y
0
Im a little confused, so i should switch the parent of the Bullets GUI to nil and then put it in the equip function? RebornedInFire 35 — 4y
0
Set it to nil instead of destroy, then set the parent to whatever it was before at the top of the equip function Ziffixture 6913 — 4y
0
I set it to player.PlayerGui.Bullets.Parent = script.Parent (Thats where the Gui is) But it doesnt resolve the issue of it breaking when reloading and clicking lmb. RebornedInFire 35 — 4y
View all comments (2 more)
0
Could you tell us what imb is? Ziffixture 6913 — 4y
0
Mouse Button 1 or Left Mouse Button. RebornedInFire 35 — 4y

Answer this question