Dragging objects with the mouse

What you can do is store a variable to keep track of how far the part is currently away from the player. If you push button X you increase that value which is the same as pushing away from the player, if you push button Y you decrease that value which is the same pulling in to the player. Whenever one of the buttons is pressed, update the value, and call a function to update the part’s position relative to the current distance.

5 Likes

Whenever I try to make a new part and put the value into the part, I cannot move the part for some reason. So if I want to make a union I can’t do that because it is making a new part even when I put the string value into the part. @starmaq

2 Likes

What are you doing exactly? The article isn’t creating a part or anything, so are you sure that you’re doing things correctly? Are the parts unanchored?

1 Like

All the settings are set the same as the original part, however when I put the value into the newly created part it simply does not work.

1 Like

Are you :Clone()ing the part and moving the cloned part? Your problem seems off-topic from this article’s premise, maybe make a #help-and-feedback:scripting-support topic.

1 Like

Forgive me as i am quite new to scripting but:

Adding a maximum range to the object seems to break everything

mouse.Target.Position = mouse.Hit.Position.Unit * 20
when I do that i get the error
players.crolaa.PlayerScripts.LocalScript:18: attempt to index nil with ‘Position’

So first i thought, hey maybe I typed something in wrong, eventually got to the point of retyping everything then just copy and pasting.

None of which fixed or helped at all.

I have no clue what is causing this and would appreciate some help.

Great tutorial by the way, Very useful stuff.

3 Likes

This means that either Target is nil, are you checking if it’s not nil?

if mouse.Target ~= nil then
2 Likes

Position setting requires you to use a Vector3 so look at doing that next.

1 Like

It is a vector3

mouse.Hit.Position.Unit * 20

does yield a Vector3

Or do you mean something else?

2 Likes

sorry my bad. I thought .unit was just a number not a normalised vector3

3 Likes

Thank you for the response, i will come back with results later.

1 Like

Just checked the script and yes I am checking if it is nil.

I’ll keep messing with stuff to find a fix

2 Likes

I’ve gotten most fixed, I’m trying to make this compatible with a R6 ragdoll script i made, only issue being is that it gets thrown into the exosphere whenever i click on them, Physics do not carry over into the part, and it isn’t as smooth as in this image:

mn9upadnk.cloudfront.net/uploads/default/original/4X/6/4/2/64252e8e46f22be64659853d6f0fd65befada680.gif

Thanks in advance!

4 Likes

Nice tutorial! It definitely helped me alot. I just want to ask how to make it so the part doesn’t go underground.

3 Likes

Thank you! Can you send a picture of it happening? Sounds like an annoying bug

2 Likes

this works perfectly! The only problem I have is that whenever I try to make it only happen to parts with a specific name, it dose not work. Is there a way to make this only start when a part has a specific name? Just so not every thing is able to be picked up.

2 Likes

you could use values and if scripts.

Never mind, I found out how to do it myself.

1 Like

awesome! thank you! i have a question though, what are the locations of the part and the things inside it? i tried reading through the tutorial but you lost me halfway. i guess inserting a script inside the part wouldnt work. lol

2 Likes

A BodyPosition and BodyGyro (even though these guys are legacy now, I should update the article) are inserted inside of the object, and destroyed after. The script doesn’t need to be inside of a part, you can just put it anywhere the client can see it, maybe StarterPlayerScripts, only thing special about the part is that it’s unanchored else the bodymovers won’t work. Also I’d appreciate it if you take a moment to explore deeper what the article is about! It wasn’t made so you can copy paste the end product, but to salvage info from it.

2 Likes