Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

2 comments

0

I've tried that and it keeps telling me Disconnect is not a valid member of PlayerMouse

0

I figured it out thanks. I rearranged my code and it works thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.