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

How can I move models with my mouse?

Ok so I have the script to move parts down:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local target = nil
local down = nil
local plr = game.Players.LocalPlayer

function gettarget()
	if mouse.Target ~= nil then
		if mouse.Target.Parent == game.Workspace[plr.Name .. "w"] then
			target = mouse.Target
			down = true
		end
	end
end
mouse.Button1Down:connect(gettarget)

function move()
	if down == true then
		if target ~= nil then
			local PosX,PosY,PosZ = mouse.Hit.p.X - mouse.Hit.p.X%1,0,mouse.Hit.p.Z - mouse.Hit.p.Z%1
			target.Position = Vector3.new(PosX,PosY,PosZ)
		end
	end
end
mouse.Move:connect(move)

function releasetarget()
	down = false
	target = nil
	mouse.TargetFilter = nil
end
mouse.Button1Up:connect(releasetarget)

But now I'm wondering how make it so it moves models (not unions).

1 answer

Lets use the :SetPrimaryPartCFrame() function to move the models

First make sure your model has a PrimaryPart set

Next let's bring in your code so we can work off of it

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local target = nil
local down = nil
local plr = game.Players.LocalPlayer

function gettarget()
    if mouse.Target ~= nil then
        if mouse.Target.Parent == game.Workspace[plr.Name .. "w"] then
            target = mouse.Target
            down = true
        end
    end
end
mouse.Button1Down:connect(gettarget)

function move()
    if down == true then
        if target ~= nil then
            local PosX,PosY,PosZ = mouse.Hit.p.X - mouse.Hit.p.X%1,0,mouse.Hit.p.Z - mouse.Hit.p.Z%1
            target.Position = Vector3.new(PosX,PosY,PosZ)
        end
    end
end
mouse.Move:connect(move)

function releasetarget()
    down = false
    target = nil
    mouse.TargetFilter = nil
end
mouse.Button1Up:connect(releasetarget)

I'm going to quickly combine your functions and where you call them together

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local target = nil
local down = nil
local plr = game.Players.LocalPlayer

mouse.Button1Down:connect(function()
    if mouse.Target ~= nil then
        if mouse.Target.Parent == game.Workspace[plr.Name .. "w"] then
            target = mouse.Target
            down = true
        end
    end
end)

mouse.Move:connect(function()
    if down == true then
        if target ~= nil then
            local PosX,PosY,PosZ = mouse.Hit.p.X - mouse.Hit.p.X%1,0,mouse.Hit.p.Z - mouse.Hit.p.Z%1
            target.Position = Vector3.new(PosX,PosY,PosZ)
        end
    end
end)

mouse.Button1Up:connect(function()
    down = false
    target = nil
    mouse.TargetFilter = nil
end)

For the Button1Down event we will check to see if 1) the mouse.target isnt nil 2) the parent of the mouse.target is a model AND that its PRIMARY PART IS SET and 3) mouse.target.parent.parent == workspace . Then setting the target as the mouse.target's parent. adding that model to the targetfilter, and setting down to true

mouse.Button1Down:connect(function()
    if mouse.Target ~= nil then
        if mouse.Target.Parent:IsA("Model") and mouse.Target.Parent.PrimaryPart~=nil and mouse.target.parent.parent==workspace then
            target = mouse.Target.Parent
			mouse.TargetFilter=target
            down = true
        end
    end
end)

For the move event we will check if target isnt nil and down == true. then we will use :SetPrimaryPartCFrame() to move the model

mouse.Move:connect(function()
    if down == true then
        if target ~= nil then
            target:SetPrimaryPartCFrame(CFrame.new(mouse.Hit.p))
        end
    end
end)

Remember we can rotate the model by adding *CFrame.Angles() at the end of CFrame.new()

finally for the button1up event, we will set down to false, target to nill, and clear out the targetfilter

mouse.Button1Up:connect(function()
    down = false
    target = nil
    mouse.TargetFilter = nil
end)

Final form

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local target = nil
local down = nil
local plr = game.Players.LocalPlayer

mouse.Button1Down:connect(function()
    if mouse.Target ~= nil then
        if mouse.Target.Parent:IsA("Model") and mouse.Target.Parent.PrimaryPart~=nil and mouse.target.parent.parent==workspace then
            target = mouse.Target.Parent
			mouse.TargetFilter=target
            down = true
        end
    end
end)

mouse.Move:connect(function()
    if down == true then
        if target ~= nil then
            target:SetPrimaryPartCFrame(CFrame.new(mouse.Hit.p))
        end
    end
end)

mouse.Button1Up:connect(function()
    down = false
    target = nil
    mouse.TargetFilter = nil
end)

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