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

Is there a way to deselect a Playermouse after getting it with Player:GetMouse()?

Asked by 7 years ago

I'm trying to make a left mouse detector on a clickdetector so you cant accidently right click it, and am using

local clickDetector = script.Parent
local buttonPressed = false

function OnClick(Clicker)
    if not buttonPressed then   
        buttonPressed = true
        local Mouse = Clicker:GetMouse()
        print("gotMouse")
        Mouse.Button1Down:connect(function()
            print("Left Click")
        end)
        wait(0.2)
        buttonPressed = false
    end
end

clickDetector.MouseClick:connect(OnClick)

but I cant figure out how to make a :disconnect() work or anything. Please help

1 answer

Log in to vote
0
Answered by
2eggnog 981 Moderation Voter
7 years ago

What you need to do is obtain a reference to the RBXScriptConnection returned by connecting your function to the event. With that reference, you can disconnect() it so the connected function doesn't run again in the future. Here's an example from the wiki:

local con
con = Part.Touched:connect(function(hit)
    con:disconnect()
    print("Collided with " .. hit.Name)
end)

In that example, the connected function will only be executed once, because it disconnects the connection immediately after the part is touched. You can apply the same concept to the Button1Down event. For more information, visit the wiki: http://wiki.roblox.com/index.php?title=RBXScriptSignal#RBXScriptConnection

0
I've tried that and it keeps telling me Disconnect is not a valid member of PlayerMouse xSantaBear 5 — 7y
0
I figured it out thanks. I rearranged my code and it works thanks! xSantaBear 5 — 7y
Ad

Answer this question