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

How to run a function as long as a player hold down mouse button1?

Asked by 10 years ago

I know I asked this before with keys, but when I do mouse.button1down, it keeps saying it being pressed when I'm not pressing it .-.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

function mousedown()
    while true do
        wait()
        local hold = false
        if mouse.Button1Down then
        print('on')
        hold = true
        end
        if mouse.Button1Up then
        hold = false
        end
        if hold == true then
        onActivated()
        end
    end
end

1 answer

Log in to vote
2
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
10 years ago

Your problem is on line 8 and 12. Simply putting mouse.Button1Down or mouse.Button1Up does not return a boolean value of true or false, rather just states that it is in fact an event of mouse (try printing mouse.Button1Down or mouse.Button1Up).

In order to check if it is being held, we will need a variable that a Button1Down event changes to true and a Button1Up changes to false:

local mouse = game.Players.LocalPlayer:GetMouse()
local button1down = false

mouse.Button1Down:connect(function()
    button1down = true
    local timedown = 0
    while button1down do
        --code
    end
end)

mouse.Button1Up:connect(function()
    button1down = false
end)
Ad

Answer this question