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

Check if mouse is down?

I'm making a fancy looking button and I was wondering how would I detect if the mouse is down?

I have this:

script.Parent.MouseButton1Down:connect(function()
		script.Parent.Frame.Position = UDim2.new(0,0,0,1)
		script.Parent.Position = script.Parent.Position + UDim2.new(0,0,0,7)
end)

EDIT By this I mean check like if its down after its clicked, say I hold my mouse down. That's what I'm looking for.

6 comments

0

Just loop until it detects MouseButton1Up.

0

good idea

0

THANK YOU

0

Or instead of looping you can write script.Parent.MouseButton1Up:wait(), unless of course a loop is necessary for something you are processing.

0

loops cause lag. I hate loops.

0

it only causes lag if you never end it.

1 answer

So, this is basically a demonstration of what was said in the comments: Using a loop and MouseButton1Up. Loops cause lag, so you could instead make an event and a BoolValue. I will cover this after the loop.


Loop

This is pretty simple, I think you will be able to understand it.

local debounce = false
local p = script.Parent

p.MouseButton1Down:connect(function()
	debounce = true
end)

p.MouseButton1Up:connect(function()
	debounce = false
end)

while wait() do -- wait() so it doesn't crash the player
	if debounce == true then -- checks to see if the value is true
		-- code for effects
	else
		-- code for disabling effects
	end
end

Event

This is a bit more complicated. You need to have a BoolValue inside the script, which detects whether the mouse is down. There is a changed event in the same script, which basically changes everything.

local val = script.BoolValue
local p  = script.Parent

p.MouseButton1Down:connect(function()
	val.Value = true
end)

p.MouseButton1Up:connect(function()
	val.Value = false
end)

val.Changed:connect(function() -- when the value changes
	if val.Value == true then -- checks to see if the value is true
		-- code for effects
	elseif val.Value == false then
		-- code for disabling effects
	end
end)

Hopefully I helped! Tell me in the comments if you need more.

~TDP

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