So here is a script that does several things when you click a text button. BUT... I have a question! So you know how building games have a gui menu and stuff for placing objects and when you click an object, the GUI closes and a partially transparent model of the selected object follows your cursor. How can I do that? I am not looking for a script but just an explanation for how to do this.
partBlueprint = game.ReplicatedStorage.Part local frame = script.Parent.Parent.Parent:WaitForChild('Items') script.Parent.MouseButton1Click:connect(function() frame.Visible = not frame.Visible script.Parent.GUI.Disabled = false script.Parent.Parent.Parent.PlacingPart.Visible = true end)
Lets use the :SetPrimaryPartCFrame() function to move the models
First make sure your model has a PrimaryPart set
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)
I agree with @DanzLua but I think it's best to use RunService while moving objects instead of using mouse.Move
game:GetService("RunService").RenderStepped:connect(function() if down == true then if target ~= nil then target:SetPrimaryPartCFrame(CFrame.new(mouse.Hit.p)) end end end)