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

Why does it keep on shootin after i unequip it?

Asked by
Xyternal 247 Moderation Voter
1 year ago

I scripted a gun, and it works perfectly fine. But the thing is, if I hold left mouse button down and unequip it at the same time, it still keep shooting. How do I fix this?


local tool = script.Parent local player = game:GetService("Players").LocalPlayer local shooting = false local mouseconnection tool.Equipped:Connect(function(mouse) mouseconnection=mouse.Button1Down:Connect(function() shooting = true while shooting do print("Mouse pressed!") 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("Neon orange") 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.Barrel.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(45) end end mouse.Button1Up:Connect(function() shooting = false end) wait(0.3) end end) end)
0
Turn the shooting value off after you unequipped. DayLighter_09 20 — 1y

1 answer

Log in to vote
0
Answered by 1 year ago
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local shooting = false
local equipped = false -- REQUIRED SO PLAYER DOESNT SHOOT WHILE UNEQUIPPED
local mouseconnection

tool.Equipped:Connect(function(mouse)
    equipped = true

    mouseconnection=mouse.Button1Down:Connect(function()
        shooting = true
        while shooting and equipped do
            print("Mouse pressed!")
            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("Neon orange")
            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.Barrel.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(45)
                end

                end

            mouse.Button1Up:Connect(function()

                shooting = false

            end)
            wait(0.3)
            end

    end)

end)

Tool.Unequipped:Connect(function()
    If shooting == true then
        shooting = false
    end

    equipped = false -- VALUE
    shooting = false -- just in case.
end)
Ad

Answer this question