Needing help making a prototype part grabbing system

Before you ask yes I am aware that DragDetectors exist, I’m just doing this as practice for my scripting skills.

Recently, I tried making a prototype of a part grabbing system that uses GetMouse(). What I attended the script to do is when holding down the mouse while hovering over a part it makes a local copy of the part with the local copy moving to the cursor when it moved. When you let go of the mouse the local part gets destroyed.

If the part is locked the part would be ignored.

(This is a localscript)

local mouse = game:GetService('Players').LocalPlayer:GetMouse()
local part = mouse.Target
local hold = false
local placement = part:Clone()

mouse.Button1Down:connect(function()
	print("mouse being held down")
	print(part)
	if hold == false and part.Locked == false then
		print("part being dragged")
		hold = true
		placement.Transparency = .8
		placement.Anchored = true
		placement.CanCollide = false
		placement.Parent = workspace
	end
end)    

mouse.Button1Up:connect(function()
	print("mouse let go")
	hold = false
	print("part placed")
	placement:Destroy()

end)

mouse.Move:Connect(function()
	if hold == true then
		placement.Position = mouse.Hit.Position
	end
end)

However, it seems that when I attempt to grab a part it goes threw the part and goes to Baseplate which is locked so it does nothing.

(This is what the script prints, I have it so that it also prints mouse.Target)
image

(Me hovering over the part I am trying to drag)

2 Likes

This local script will drag any object you select that isn’t locked and drag it until release which will delete it.

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local part = nil
local hold = false
local placement = nil

mouse.Button1Down:Connect(function()
	part = mouse.Target
	print("mouse being held down")
	print(part)
	if hold == false and part.Locked == false then
		print("part being dragged")
		placement = part:Clone()
		hold = true
		placement.Transparency = .8
		placement.Anchored = true
		placement.CanCollide = false
		placement.Parent = workspace
	end
end)

mouse.Button1Up:Connect(function()
	print("mouse let go")
	hold = false
	print("part placed")
	placement:Destroy()
end)

mouse.Move:Connect(function()
	if hold == true then
		placement.Position = mouse.Hit.Position + (character:WaitForChild("HumanoidRootPart").CFrame.LookVector * 5)--this makes it so that the object will be in front of the player
	end
end)
2 Likes

Your solution works but I wanted to point out a nitpick. The object isn’t centered. The issue seems to be with how you tried to solve a “bug” in my code with the “(character:WaitForChild(“HumanoidRootPart”).CFrame.LookVector * 5)”. I am thankful that you wanted to help me with that but I mean, it looks funny.

One second I will try to fix it

1 Like

I FOUND A FIX!!!

Just set mouse.TargetFilter = placement

I am still keeping your post as a solution because I didn’t even ask you to fix that!

Thanks for trying tho!

This is what the updated code looks like:

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local part = nil
local hold = false
local placement = nil

mouse.Button1Down:Connect(function()
	part = mouse.Target
	print("mouse being held down")
	print(part)
	if hold == false and part.Locked == false then
		print("part being dragged")
		placement = part:Clone()
		hold = true
		placement.Transparency = .8
		placement.Anchored = true
		placement.CanCollide = false
		placement.Parent = workspace
		mouse.TargetFilter = placement -- prevents part from flying into the camera
	end
end)

mouse.Button1Up:Connect(function()
	print("mouse let go")
	hold = false
	print("part placed")
	placement:Destroy()
end)

mouse.Move:Connect(function()
	if hold == true then
		placement.Position = mouse.Hit.Position
	end
end)
1 Like

Oh okay great, sorry that I wasn’t able to get a solution faster.
Edit: You should also pin your fix as the answer incase someone has a similar issue since it works 100%.

1 Like

It’s totally ok! In fact, it’s kinda important to learn how to solve bugs. This simple fix was good practice.

1 Like

How do I do that? I don’t see a “pin” option.

1 Like

I have no idea as I’m new to the defourm, but maybe you can try unpinning if possible and pin again. Maybe you can only pin once

Me too, I just got the ability to reply and make topics. From my understanding you can only have one solution. I don’t want to untick your solution as you did assist me.

Edit: What I am saying is that I think you are confusing “Pinning” with the highlight solution feature.

1 Like

yeah okay I understand now…(30 c limit)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.