How to detect mouse being held

local UIS = game:GetService("UserInputService")
local mouse = game.Players.LocalPlayer:GetMouse()
while true do

UIS.InputBegan:Wait()
	if UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
		repeat
		local pos = mouse.Hit.Position
		script.Parent:WaitForChild("Humanoid"):MoveTo(pos)
			print("moved")
		until UIS.InputEnded
	end
wait(0.1)
end

The script should move the humanoid to the mouse’s position as long as the player is holding the mousebutton1 down, but it doesn’t work, it only moves for like half a second and then stops.

2 Likes

An easy approach

local UIS = game:GetService("UserInputService")
local mouse = game.Players.LocalPlayer:GetMouse()
local holding = false

UIS.InputBegan:Connect(function()

	if UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
	
		holding = true
	
	end

end)

UIS.InputEnded:Connect(function()

	if not UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then

		holding = false

	end

end)

while true do

	wait()
	if holding == true then
	
		local pos = mouse.Hit.Position
		script.Parent:WaitForChild("Humanoid"):MoveTo(pos)
		print("moved")
		
	end

end
1 Like

Yay that worked :shallow_pan_of_food: :shallow_pan_of_food: