A Draggable Part

Hey there, Dev

So I want to make a Cooking System but I would like to start off with draggable foods that you can just drop on the stove and cook it, BUT I’m having some issues :confused:

So I want to make the part fall when you release the mouse button, I’m still trying to question myself to what to do but that may take a while because I get lost easily xd anyways, the part keeps sticking to the mouse.Hit.Position and I can’t find a way to “unstick it” (that is so dumb lol)
Here’s the code:

local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local Part = workspace.Part
Mouse.TargetFilter = Part
local OriginalPos = Part.Position
local partMove

Part.ClickDetector.MouseHoverEnter:Connect(function()
	Mouse.Button1Down:Connect(function()
		Part.Position = Mouse.Hit.Position
	end)
	Mouse.Button1Up:Connect(function()
		Mouse.Move:Connect(function()
			Part.Position = OriginalPos
		end)
	end)
end)

Anyways I hope you can help :smiley:
-Underrated/Dev

2 Likes

This topic may help:

1 Like

I did some looking up on the dev forum first and I didn’t see anything like this, I’ll read up on it.
Thanks

so first problem is that you are using mousehoverenter to create connections, if a player repeatingly has there mouse leave the block and reeneter you’ll have multiple of these connections, making several functions fire, I would just get rid of the mouse hover enter entirely

the easiest way to do this is making your own custom mouse position thing, you can accomplish this with raycasting

local UnitRay = Mouse.UnitRay
local NewRay = Ray.new(UnitRay.Origin,UnitRay.Direction*1000)
local _,Pos = workspace:FindPartOnRayWithIgnoreList(NewRay,{Part,Character})
return Pos

The reason i recommend this is because you can set the part into the ignorelist of the ray, this will make it where the position won’t constantly change

you can use the Pos variable to position the part, also make sure to disconnect the mouse.move whenever you stop moving the part

Personally instead of mouse.move I would use a while loop that keeps running while the user is holding X input

You say that the part keeps sticking to your mouse when you let go, that because your mousemove is in ur button1up connection

Thanks for the help, I’ll look over the script and try to fix it with your tips :DD
I don’t even know Raycasting as yet xd, but I’m planning to learn it soon after my big projects

Clone the part instead of assigning the position on the Button1Down, it will get overriden when the mouse is moved so it will look like nothing happened… part:Clone().Parent = workspace