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

How do i detect if someone is holding down left mouse button?

Asked by
Xyternal 247 Moderation Voter
1 year ago
Edited 1 year ago

So I was looking at the TechwithMike tutorial for how to make a gun (using only a local script) and I pretty much found what i was looking for. But i want to make sure that it shoots every time the player holds the left mouse button down. I think it has to do something with while loops, but idk. My current code:

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local LeftButtonDown
tool.Equipped:Connect(function(mouse)

    mouse.Button1Down:Connect(function()

        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("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(30)
            end
        end
    end)
end)

EDIT: Im also trynna make rifles so I tried to add debounce and it didn't work. What am i lacking?

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local debounce 
local detime = 2
tool.Equipped:Connect(function(mouse)
debounce = false
    mouse.Button1Down:Connect(function()
        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("Yellow")
        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(30)
            end
        end
    end)
    debounce = true
    wait(detime)
end)
0
i got an answer to my first problem, just need help with the second now Xyternal 247 — 1y

1 answer

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

For the debounce question, the reason why your debounce is not working is because you never check if the debounce is true or false. You can check this in your mouse.Button1Down function.

Other small things to note, make sure to put the mouse.Button1Down function in a variable, so that when the tool is unequipped, you can disconnect the function. This can prevent memory leaks, and use task.wait() instead of wait().

Also, don't use the parent argument of Instance.new.

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local debounce = true
local detime = 2

local mouseConnection
tool.Equipped:Connect(function(mouse)

    mouseConnection = mouse.Button1Down:Connect(function()

        if debounce == true then
            debounce = false

            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")
            beam.BrickColor = BrickColor.new("Yellow")
            beam.FormFactor = "Custom"
            beam.Material = "Neon"
            beam.Transparency = 0.25
            beam.Anchored = true
            beam.Locked = true
            beam.CanCollide = false
            beam.Parent = workspace

            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

            task.wait(detime)
            debounce = true
        end

    end)

end)

tool.Unequipped:Connect(function()
    mouseConnection:Disconnect()
end)
Ad

Answer this question