I am trying to make a script that allows players to repair something by holding down the mouse button, but my mouse isn't getting detected. Why is this?
print("Phone box active") PhoneBox = script.Parent Broken = PhoneBox.Broken CD = Broken.ClickDetector UserInput = game:GetService("UserInputService") RepairVal = PhoneBox.LocalScript.RepairVal CD.MouseClick:connect(function(playerWhoClicked) print("Broken clicked.") UserInput.InputBegan:connect(function(key) print("Input began") if key.UserInputType == Enum.UserInputType.MouseButton1 then print("Mouse button") repeat wait(0.1) RepairVal.Value = RepairVal.Value + 0.1 until not UserInput:IsKeyDown(Enum.UserInputType.MouseButton1) and playerWhoClicked.Character end end) end)
There are a lot of different problems in this one script. Firstly, a LocalScript won't run at all as a child of a Model or Part in Workspace. LocalScripts have to be in your Player's PlayerGui or PlayerScripts, which means the script is usually placed in StarterGui or StarterPlayerScripts. And you can't use a Script either, since that will execute on the server where none of this code is valid.
Secondly, the first click on the PhoneBox will add the user input listener, but that's all it will do, and it will happen on Mouse Up. The event handler won't fire until you click again, but...
The 3rd problem is that this UserInputService listener you're hooking up will fire on ALL mouse button 1 mouse down events no matter where you click, not just on the PhoneBox! This is certainly not what you want.
Problem #4 is that IsKeyDown doesn't work with mouse events, you need to use UserInputService:IsMouseButtonPressed, or you'll get an error about not being able to cast the token (enum value is what it means).
Issue #5 is that you shouldn't really use a repeat or while loop that polls for the state of the mouse button. 0.1 seconds is slow enough that you could miss mouse events entirely. Normally, you'd listen for the mouse button InputEnded.