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)
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)
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.
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)
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%.
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.