How to make if mouse is clicked

Hello,
I am trying to make a script, that when mouse click is activated, a function.
If someone stop holding the mouse click button, it will stop.

game.Workspace.Part.ClickDetector.MouseClick:Connect(function()
print("worked")
end)

Does anyone know how could i make it? - Thanks :slight_smile:

1 Like

add a variable to the function then check if that is in use!

game.Workspace.Part.ClickDetector.MouseClick:Connect(function(click)
 if click then - alternativly you can use "While click" -- meaning when it is clicked do X 
- code here!
end
end)

No, once you stop holding the mouse click, it will print worked. As long you are holding the button, nothing is happening

Does not work. Also by while click, should i make it while click do?

while click do
print("worked")
end

yes while click do as long as you have that variable in the function match the variable you have in the while loop it should work!

That does not work. Still, as long i release the mouseclick button, it works

Try this:

local Part = script.parent
local ClickDetector = Part:WaitForChild("ClickDetector")
ClickDetector.MouseClick:connect(function(MouseClick)
print("works")
end)
1 Like

adding a waitforchild isn’t needed due to the fact we know its parented to the part so this would in tern slow the game down and is bad for the games performance!

That was the way I learned how to do click detectors, and they always work for me. :smiley:

it is much faster to use task.wait() as that is a built in function to the studio which is more upto date and has modern techniques to wait faster then the waitforchild which is primitive tech in this use case!

A “MouseClick” is considered as the mouse being pushed down and then released upon which the event fires. If you do not want this behaviour then a ClickDetector's MouseClick is not for you.

I know a bit more complicated way for this to work.
So to know if player is holding LMB/RMB or not you have to:

(Note - This is Local-Sided)

local mouse = game.Players:GetMouse()
local holding = false

mouse.Button1Down:Connect(function() -- 1 for LMB and 2 for RMB
    holding = true
end)

mouse.Button1Up:Connect(function()
    holding = false
end)

To get values from Server-Side you can use RemoteFunctions
Or continuously fire RemoteEvent to Server-Side.
(It’ll activate every time the player clicks Mouse Buttons, make an “if” check to avoid it.
Pass the values only when the mouse is at the certain part)