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

How do I get the position of the mouse while it is being held down?

I am trying to make a script that a part will float to the position of where the mouse is being held down. However, I can't find anyway to get the position of the mouse while it's being held down, only where it clicks. Is it even possible to get the position of where the mouse is while it is held down?

This is what I've tried and it only shows where the mouse clicks:

	
	
	--// Services
local UserInputService = game:GetService("UserInputService")

--// Objects
local Held = false

UserInputService.InputBegan:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
				Held = true

		local plr = game.Players.LocalPlayer
		local Mouse = plr:GetMouse()
local MouseCFrame = Mouse.Hit
local MousePosition = MouseCFrame.p
print (MousePosition)
	end
end)

UserInputService.InputEnded:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		Held = false
	end
end)


1 comment

0

get rid of "Held" and change the if to a while

1 answer

Your script is good, but to make the part constantly follow the player use this method:

    --// Services
local UserInputService = game:GetService("UserInputService")

--// Objects
local Held = false

UserInputService.InputBegan:Connect(function(inputObject)
    while inputObject.UserInputType == Enum.UserInputType.MouseButton1 then -- sets the position on a constant loop until conditions are no longer met.
print("firm clasp") --debugging statement
                Held = true

        local plr = game.Players.LocalPlayer
        local Mouse = plr:GetMouse()
local MouseCFrame = Mouse.Hit
local MousePosition = MouseCFrame.p
print (MousePosition)
wait() --prevents script for crashing
    end
end)

UserInputService.InputEnded:Connect(function(inputObject)
    if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
print("ungrip") --debugging statement
        Held = false
    end
end)

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