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

looping question about looping the mouse button?

Asked by 6 years ago

how do i make a loop that activates by pressing the mouse button until you release it.

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer

tool.Equipped:connect(function(mouse)
    print("Tool equipped!")

    mouse.Button1Down:connect(function()
        print("Mouse pressed!")
        local ray = Ray.new(tool.Crystal.CFrame.p, (mouse.Hit.p - tool.Crystal.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

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

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

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

1 answer

Log in to vote
0
Answered by 6 years ago

The basic format would be something like this:

local mouseDown = false

local function onMouseLeftButtonDown()
    mouseDown = true
    while mouseDown do
        --Your code here
        wait()
    end
end

local function onMouseLeftButtonUp()
    mouseDown = false
end

game.Players.LocalPlayer:GetMouse().Button1Down:Connect(onMouseLeftButtonDown)
game.Players.LocalPlayer:GetMouse().Button1Up:Connect(onMouseLeftButtonUp)
Ad

Answer this question