Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
zValerian 108
5 years ago

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)
0
get rid of "Held" and change the if to a while DeceptiveCaster 3761 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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)
Ad

Answer this question