Dragging objects with the mouse

The legend of scripting is back to teaching us!

I have a question, I’m having a hard time to understand BodyPosition and BodyGyro. Devforum doesn’t help me out, is there a better explanation? Please provide examples about comparison so I can understand better.

1 Like

Thanks for this tutorial! I was about to make my own code but now I don’t have to

2 Likes

Hi! Very late to the party, sorry.

I do recommend using AlignPosition and AlignOrintation instead, and they are fairly easy to use. Simply specify a force, hook up 2 attachments, the first is the object you want to align, the second is the align to.

1 Like

I tried the script and I think it’s easy to understand for beginners like me except that mouse module of yours, oh my

I do have one question. In the script, you wrote a comment about adding a check to see if the object is falling or not so that we can set NetworkOwnership to the server when we stop dragging the object so that it won’t freeze in mid air. How do we check if it’s falling or not? I thought about BasePart.Velocity, but on BasePart.Velocity (roblox.com), it said it was deprecated. I don’t know how else to go about this.

Would it also still be okay to just not set the NetworkOwnership back to the server? It didn’t make much of a difference when I tried it and the part moved very smoothly without the mid-air freeze. It moved smoothly on both screens when I did a 2 player test as well.

2 Likes

Just this testing won’t do much, for example try driving a server-powered car with 2 players in a server… not gonna spot much right?
If you see ANY sort of cutting or lag then this is a red flag for lower-end users and real ingame scenarios. Still having client ownership over un-anchored parts is a good idea to prevent massive server flood, but it has have optimization for example when you have like 10k parts, all moving and replicating to the server.

Not going go too deep on optimization because you don’t always need it, but you at least need a reasonable code that won’t annoy people when they are doing any activity. You want to do everything as fast and simple as possible!

1 Like

What do you mean by cutting or lag?

It’s when the server activates it’s anti-lag features such as part temporary anchoring. I’m not too experienced in this topic, but it is possible to get a part stuck in the air yet unanchored.
Not only that, when having a decent/bad connection or device, waiting for the server to pick up information and send you the data you need is not the perfect way to play, especially if motion is involved.

So, is a part freezing in mid air while switching NetworkOwnership, also considered cutting or lag?

Hey! Sorry for the late response.

Indeed, transferring the network ownership back is a tedious process. in the version of the script I supplied I said to check if the mouse is no longer moving, and good job deciding to use BasePart.Velocity for that! Yes, it has been (very) recently deprecated, replaced by :GetVelocityAtPosition() (I think you need to pass the position of the dragged part as an argument). Constantly checking if the velocity is equal to the zero vector Vector3.new(0, 0, 0), and if so proceed to give back ownership to the server.

In the supplied script, for some reason I was too lazy to do this, and instead I put a wait. This is super bad, if I drag the part again while the wait still didn’t finish yielding, the ownership will be transferred back mid-dragging, and if I stop dragging the ownership will be transferred to the player now. Chaos!

But even with the velocity check, perhaps that isn’t so perfect. I can think of scenarios where some difficulties may occur. For example, if you stop dragging the part and it drops on a conveyor belt, the part will still have a velocity, hence the ownership is not transferred unless the part is no longer on the belt.

I ended up making an even better version of the script for a project of mine, where I ditched this velocity idea, and very simply decided to constantly check if the mouse is no longer within a certain distance of the character (of course this distance is way greater than the distance of the dragged object from the head). With a bit of fast spawning, this works wonderfully and is way simpler!

I will add this newer version to the article in a second. (too lazy to comment, sorry!)

even_better_dragging.rbxl (31.4 KB)

except that mouse module of yours, oh my

and yes, this uses some magical fancy wrapping shenanings hehe. Here is a page about it, don’t really think about it too much, I only incorporated it due to the suppression of the PlayerMouse.

3 Likes

I’m not quite sure what’s causing the mid air freezing happening, but your reason sounds very solid.

Another theory I have is perhaps when you switch the NetworkOwnership the server might need sometime to pick up from where the client did to the part, so yeah. I have a good computer and decent connection, so I don’t think this is a problem for low-end users too.

3 Likes

Hey Starmaq!

I’ve been reading this post for a long time now and I have a quick but yet rather confusing question. What would you have to change in the script in order to have the Object/Part floating in the air without falling and being able to drag it around again? I tinkering a little bit with the script but found nothing. I figured if I was gonna get some sort of help, it would be the guy who created the script. :slight_smile:

Please feel free and help me with this problem of mine! Thank you!

Here is a video example:

2 Likes

Its all to do with gravity.
You need to apply an upward force that’s equivalent to the downward force due to gravity.

3 Likes

I applied an upward and downward force to my part like you said but now a bigger problem comes to play. The part keeps moving due to the the gravity and wont stop. What I want is the part to freeze and not move when I let go of the part.

Script:

local part = script.Parent
local downForce = -part:GetMass()*workspace.Gravity
local force = -downForce
local bodyForce = Instance.new(“BodyForce”)
bodyForce.Force = Vector3.new(0,force,0)
bodyForce.Parent = part

Video example:

1 Like

Have you tried adjusting the force value so it is just less than gravity so the part hovers.

2 Likes

I actually scarped the script and went with using Body Velocity and it works out better than before. Now that we completed one of our problems, we have one remaining. How do we stop it from spinning?

Video:

Edit:
I applied both body velocity and Body Gyro in my part. Now it does not spin or move on its own

1 Like

Also, just wanted to point out that a player can drag another player.

What your seeing is in fact my framerate but also how laggy the player moves. hahahaha such a funny glitch… ummmm… how do you fix this? (not the lag but the dragging of the player)

1 Like

I believe the drag player is just doing what you asked.
That is ‘move this part’.
The player is just another part.

1 Like

Okay, but I have no idea how to make the player not a part. What do I change? What do I add? What do I do?

1 Like

In order to be able to move a part you must be using a click detector in your script to connect to it so change the script not allow the connection to parts that have a Humanoid child.

2 Likes

I can’t possibly switch out the entire script just because of a click detector. And when you say

Do you mean with or without the Click Detector?

1 Like