Dragging objects with the mouse

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

okay, so I need to insert a bodygyro and body position, I though those were automatically by the script, that’s why. So I’m guessing since I can place the script anywhere that means I should somewhere address the parts location?
I’m also pretty new/bad at scripting so I was wondering if the different addressed script parts should be merged together or the last script is the final product?

1 Like

You see, you don’t need to address each part in the game. Each time the player grabs a part, that part is given to the script by mouse.Target. Then any operation you want to do on the part, you edit mouse.Target.

As for your second question, the script is a stand-alone thing, it’s indepedent on the parts you use. In this article I don’t cover how you would restrict grabbing certain parts, but to do that you can just name parts that are supposed to be dragged a special name, and when Button1Down is fired you check if the current clicked part is named that special name.

2 Likes

but for me the script doesnt work, i used the last script and was confused at first on some things but this explanation helped me. though, even when the script is inside a part it doesnt work, i guess its location is wrong?

1 Like

I told you in the previous reply that it’s supposed to be placed somewhere in the client, doesn’t necessarily have to be a part. Place it in StarterGui for a start.

1 Like

its in startergui, is it supossed to be a local or normal script? also, is the last script you mentioned in the OP post the finished one?

Me and my friend used this and I say good job. For anyone wondering how to drag a model just weld it.

1 Like

am i able to just copy paste the last part of code?

For me it only works in Studio

Do you have to be R6, and does it being in a localscript matter? (It’s in StarterPlayerScripts as a localscript)

Did I miss something?

local mouse = player:GetMouse() 
local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService") --we will use this service for something later
local target 
local down

function mouseHit(distance) 
	local ray = Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction * distance) --as you can see, here creating a ray with the same origin (which is the camera of course) and the same direction BUT longer, whatever the distance parameter is
	local _, position = workspace:FindPartOnRay(ray)

	return position 
end

if mouse.Target ~= nil then
mouse.Target.Position = mouseHit(20)
mouse.Target.Position = camera.CFrame.Position + (mouse.Hit.Position.Unit * 20)
end


mouse.Button1Down:connect(function() 
	if mouse.Target ~= nil and mouse.Target.Locked == false then
		target = mouse.Target 
		mouse.TargetFilter = target 
		down = true 

		local gyro = Instance.new("BodyGyro") --adding the forces
		gyro.Name = "Gyro"
		gyro.Parent = target 
		gyro.MaxTorque = Vector3.new(500000, 500000, 500000)
		local force = Instance.new("BodyPosition") 
		force.Name = "Force" 
		force.Parent = target
		force.MaxForce = Vector3.new(10000, 10000, 10000) --you may wanna modify this a bit, since it effect if you can move an object or wrong depending on its weight/mass (in other words, the force might not be strong enough)
	end
end)

game:GetService("RunService").RenderStepped:Connect(function() --replaced the move event with renderstepped because it works better in some cases, renderstepped is an event that fires every frame, basically super fast, look it up it is important!
	if down == true and target ~= nil then 
		target.Gyro.CFrame = target.CFrame --this is to keep rotation
		target.Force.Position = camera.CFrame.Position + (mouse.UnitRay.Direction * 20)
	end 
end) 

mouse.Button1Up:connect(function() 
	if target then --of course we wanna remove the forces after the dragging, first check if there was even a target
		if target:FindFirstChild("Gyro") or target:FindFirstChild("Force") then --check if there was a force
			target.Gyro:Destroy() --DESTROY!!!!
			target.Force:Destroy()
		end
	end
	down = false
	mouse.TargetFilter = nil
	target = nil 
end)```

A small nitpick: There are some inconsistent naming of Connect. You should name all of them to Connect to avoid confusion and that way, many newbies viewing this article will not use deprecated functions.

1 Like