Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to create draggable parts like building tools?

I have a scripted tool that lets me create parts then move them as accordingly to my desire but I want to be able to allow dragging like real functional building tools. Let's say I have a part which is one of the spheres (that represents forward), how would I make it that if I wanted to drag it, it would keep moving it?

Here is a basic idea of my script, it should be simple to understand:

local part = Instance.new("Part")
part.Position = Vector3.new(0, 5, 0)
part.Size = Vector3.new(2, 2, 2)
part.Parent = workspace
local sphere = Instance.new("Part")
sphere.Name = "LookVectorSphere"
sphere.Position = workspace

local increment = 1

local function movePartForward()
	part.Position = part.Position + (part.CFrame.LookVector*1)
	sphere.Position = sphere.Position + (part.CFrame.LookVector*1)
end

This is just an example to make it clear and easy of what I'm asking so I will only be asking for that one sphere example I gave.

2 answers

✓ Accepted answer

Fixed script from user bostaffmanbulgaria1 Who he stole from a guy that didn't know anything about scripting. Go ahead and use this!

-- The following script allows you to move the part, but not Up in the Y axis because I'm too stupid for this one...

----------------
-- LocalScript
----------------
local PlayerMouse = game.Players.LocalPlayer:GetMouse()
local DraggablePart = game.Workspace.DraggablePart
local PartPositionEvent = script.PartPositionEvent
local Draggable = false
local Target = nil


PlayerMouse.Button1Down:Connect(function()
    Draggable = true
    repeat
        wait()
        if PlayerMouse.Target == DraggablePart then
            Target = PlayerMouse.Target
            Target.Position = Vector3.new(PlayerMouse.Hit.X, PlayerMouse.Hit.Y, PlayerMouse.Hit.Z)
            PartPositionEvent:FireServer(Target, Target.Position)
        end
    until Draggable == false
end)


PlayerMouse.Button1Up:Connect(function()
    Draggable = false
end)
----------------------------------------------------------------------------------------------------------
-- Inside the Local Script put a Remote Event

-------------------
-- ServerScript
-------------------
script.Parent.OnServerEvent:Connect(function(player, Target, PartPosition)
    Target.Position = PartPosition
end)
--[[
 You basically get the PlayerMouse's Target and if the target is THIS PART then
 allow the Position of this target to be the Hit of the mouse which the Hit is 
the position of THIS PART...

Well. That about covers what I can do for you.
--]]


2 comments

0

Well If you don't know something then Google is your friend as always. I see that you've added "PlayerMouse.Hit.Y" which I did not add for a reason. Whenever the Part is being dragged it comes to the position of the camera. Basically like a following NPC. That is why I used a number instead of Mouse.Hit.Y

0

No. Its just because you used google and was a lazy sack of shit and just wanted reputation.

-- The following script allows you to move the part, but not Up in the Y axis because I'm too stupid for this one...

----------------
-- LocalScript
----------------
local PlayerMouse = game.Players.LocalPlayer:GetMouse()
local DraggablePart = game.Workspace.DraggablePart
local PartPositionEvent = script.PartPositionEvent
local Draggable = false
local Target = nil


PlayerMouse.Button1Down:Connect(function()
	Draggable = true
	repeat
		wait()
		if PlayerMouse.Target == DraggablePart then
			Target = PlayerMouse.Target
			Target.Position = Vector3.new(PlayerMouse.Hit.X, 1, PlayerMouse.Hit.Z)
			PartPositionEvent:FireServer(Target, Target.Position)
		end
	until Draggable == false
end)


PlayerMouse.Button1Up:Connect(function()
	Draggable = false
end)
----------------------------------------------------------------------------------------------------------
-- Inside the Local Script put a Remote Event

-------------------
-- ServerScript
-------------------
script.Parent.OnServerEvent:Connect(function(player, Target, PartPosition)
	Target.Position = PartPosition
end)
--[[
 You basically get the PlayerMouse's Target and if the target is THIS PART then
 allow the Position of this target to be the Hit of the mouse which the Hit is 
the position of THIS PART...

Well. That about covers what I can do for you.
--]]


1 comment

0

What if i want it to be incrementable? This seems to move too precisely? Would I use math.floor?

This discussion is preserved as it was. New answers, comments, and votes are closed.