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

My drag part LocalScript isn't working, anyone know why?

Asked by 6 years ago

So when the user clicks and holds on a part, that object follows the mouse. But I only want it to be able to move specific parts. Anyone know why this isn't working?..

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local down
local mtarget

function clickObj()
if mouse.Target ~= nil then
mtarget = mouse.Target
print(mtarget)
down = true
mouse.TargetFilter = mtarget
print(mouse.TargetFilter)
end
end
mouse.Button1Down:connect(clickObj)

function mouseMove()
if down and mtarget and not mtarget.Name == "MoveablePart" then
local posX,posY,posZ = mouse.hit.X, mouse.hit.Y, mouse.hit.Z
mtarget.Position = Vector3.new(posX,posY,posZ)

end
end
mouse.Move:connect(mouseMove)

function mouseDown()
down = false
mouse.TargetFilter = nil

end
mouse.Button1Up:connect(mouseDown)
0
it'll be a while but i can tell you what you're doing wrong just hold on (like 10 - 20 mins) mattchew1010 396 — 6y
0
Aye, thank m8! andyad13 74 — 6y
0
Thanks* andyad13 74 — 6y
0
nevermind, figured it out. Thanks anyway! andyad13 74 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

this is really buggy actually sorry :( but heres what i have so far local script:

local mouse = game.Players.LocalPlayer:GetMouse()
local Click = false
mouse.Button1Down:Connect(function() Click = true end)
mouse.Button1Up:Connect(function() Click = false end)
local loop = true
local target
mouse.Move:Connect(function()
if loop == false and Click == true then
    if mouse.Target then
        target = mouse.Target
    local x,y,z = mouse.Hit.x, mouse.Hit.Y, mouse.Hit.Z
game.ReplicatedStorage.NewPos:FireServer(Vector3.new(x,y,z), mouse.Target)
end
end
end)
while true do
    if mouse.Target == nil then
        game.ReplicatedStorage.StopDragging:FireServer(target)
        Click = false
    else
        target = mouse.Target
    end
    if Click == false then
        game.ReplicatedStorage.StopDragging:FireServer(target)
    end
    if Click == true and loop == true then
        if mouse.Target then
            if mouse.Target:FindFirstChild("Draggable") then
                local x,y,z = mouse.Hit.x, mouse.Hit.Y, mouse.Hit.Z
            game.ReplicatedStorage.StartDragging:FireServer(Vector3.new(x,y,z), mouse.Target)
            loop = false
            end
        end
    end
    wait()
end

serverScript:

local dragger = script.Dragger:Clone()
game.ReplicatedStorage.StartDragging.OnServerEvent:Connect(function(plr, Position, Object)
    local weld  = Instance.new("Weld")
    local bp = Instance.new("BodyPosition")
    if Object:IsA("Model") then
        weld.Part0 = Object.PrimaryPart
        dragger.Parent = Object
        weld.Part1 = dragger
    else
        weld.Part0 = Object
        dragger.Parent = Object
        dragger.Position = Object.Position + Vector3.new(Object.Size.X + 0.05, Object.Size.Y + 0.05, Object.Size.Z + 0.05)
        weld.Part1 = dragger
    end
    bp.Parent = dragger
    bp.Position = Position
    weld.Parent = dragger
end)
game.ReplicatedStorage.NewPos.OnServerEvent:Connect(function(plr, Position, Object)
if Object:FindFirstChild("Dragger") then
    Object.Dragger.BodyPosition.Position = Position
end
game.ReplicatedStorage.StopDragging.OnServerEvent:Connect(function(plr, object)
    if Object:FindFirstChild("Dragger") then
        Object.Dragger:Destroy()
    end
end)


end)
Ad

Answer this question