Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to create draggable parts like building tools?

Asked by
aredanks 117
4 years ago

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

Log in to vote
1
Answered by
Lakodex 711 Moderation Voter
4 years ago

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.
--]]


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 bostaffmanbulgaria1 89 — 4y
0
No. Its just because you used google and was a lazy sack of shit and just wanted reputation. Lakodex 711 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
-- 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.
--]]


0
What if i want it to be incrementable? This seems to move too precisely? Would I use math.floor? aredanks 117 — 4y

Answer this question