Making a part follow the mouse and behave as it does in Roblox Studio

So I’m making a part follow the mouse, but i want the part to not be intersecting with the floor and with walls.
imageimage

The script I used is this:

CalculatedHitPos = Vector3.new(math.floor(hitPos.X), math.floor(hitPos.Y), math.floor(hitPos.Z))

Is there a way to make it behave as studio does?

2 Likes

Yeah you dont use .Origin, you use mouse.Hit. (mouse.Origin is where the the mouse its at when it runs)
Anyway, I’ve already done that, what i want to achieve is this:
imageimage
As you can see, it doesn’t intersect with the parts.

Yeah, sorry. I didn’t read it fully and I thought Hit was the part the mosue was over.

1 Like

You could check out the Dragger instance.

It’s not a recreation of the system if you want it to have special behavior, however if you don’t care for custom behavior, you could check it out.

You can use MouseDown() and MouseUp() to tell it when to start dragging.

2 Likes

Oh I see. I didnt know that was a thing. Tysm!!

1 Like

Can you give me an example? There doesnt seem to be much documentation about it honestly.

First you must create the Dragger instance (not a service, my mistake):

local dragger = Instance.new("Dragger")

Then on mousebutton1down call MouseDown:

dragger:MouseDown(PartToBeMoved, Vector3.new(0, 0, 0), {PartToBeMoved)

Then on mousebutton1up call MouseUp:

dragger:MouseUp()
1 Like

What is Vector3.new(0,0,0) supposed to do here?

Not sure myself, I found it from a free model (because I could not find any other resources). I guess it is an offset from the part’s origin.

1 Like

PLEASE NOTE: If this is for a tool, make sure the position of the part is being sent to the server every time the mouse is moved, otherwise it will not sync.

You can do this via a RemoteEvent. When the player moves their mouse (mousemove), call FireServer on the event with the new position of the part. (If a part is being dragged.)
And on the server, set the part’s position.

i.e.

LocalScript

local draggingPart = nil

mouse.Move:Connect(function()
    if draggingPart then
        remoteEvent:FireServer(mouse.Position, draggingPart)
    end
end)

mouse.MouseButton1Down(function()
    draggingPart = mouse.Target
end)

mouse.MouseButton1Up(function()
    draggingPart = nil
end)

ServerScript

remoteEvent.OnServerEvent:Connect(function(pos, part)
    part.Position = pos
end)
1 Like

Dont worry, the way im doing my script doesnt even use the MouseButton1Up. This is the code

local function updateBlacklistFolder(object)
    dragger:MouseUp()
    for _, v in pairs(BlackListFolder:GetChildren()) do
        v:Destroy()
    end
    local newObject = object:Clone()
    newObject.Parent = BlackListFolder
    local toReference = Instance.new("Part")
    toReference.Parent = BlackListFolder
    toReference.Name = "toReference"
    dragger:MouseDown(toReference, Vector3.new(0, 0, 0), {toReference})
end

but it sayys dragger:MouseUp() cannot be casted unless mouseDown has. So how to check it its down?

I don’t understand where you are calling MouseDown. Why is it in updateBlacklistFolder and not in MouseButton1Down?

When is updateBlacklistFolder called?

ideally, you could store it in a variable.

local dragging = false
local function updateBlacklistFolder(object)
    if dragging then dragger:MouseUp() dragging = false end
    for _, v in pairs(BlackListFolder:GetChildren()) do
        v:Destroy()
    end
    local newObject = object:Clone()
    newObject.Parent = BlackListFolder
    local toReference = Instance.new("Part")
    toReference.Parent = BlackListFolder
    toReference.Name = "toReference"
    dragging = true
    dragger:MouseDown(toReference, Vector3.new(0, 0, 0), {toReference})
end

ive done that just now… the position is not really changing tho
The folder was so i can have blacklist to use mouse.Hit honestly (which might not be that useful now) and was updated every time i wanted to change the part i wanted to move. The idea was placing a part there and move it with a bodyPosition. So what I’m trying to do is make the part being dragged and make the bodyposition follow it so it has a smooth movement.
Also, I want this to be local, not serversided

I also forgot, you must call MouseMove in dragger when the mouse moves.

(Make sure to check that it is dragging.

if dragging then
    dragger:MouseMove(mouse.UnitRay)
end

)

should I put that in a runservice.Stepped?

Mouse.TargetSurface, RunService.RenderStepped and Part.Position = mouse.Hit.X + Part.Position.X / 2 -- Y for floor and X/Z for walls

1 Like

No, in your mouse’s move event

Move event is inacurrate, because if you move the character and not the mouse, the part will not change the position, i know this can help or not but it’s good to contribute.

That’s true, but connecting to renderstepped and firing an event every tick is quite wasteful. Mouse move only fires when the mouse is moved (a lot less than renderstepped).

Also your code above

does not work for me. As I move the part, it moves farther away from my mouse.