Part moving with mouse issues

It works, but How would I change it to work only on the z and x axis

Something like this:

part.Position = mouse.Hit.Position * Vector3.new(1,0,1)

finally it works for you brother i am so happy :slight_smile:

thanks it works but a few bugs, when moving it goes half way into the ground and you can only stop moving it sometimes

You can offset the position like this:

part.Position = mouse.Hit.Position * Vector3.new(1,0,1) + Vector3.new(0,part.Size.Y/2,0)

Devforum telling me I made 20% of replies in this topic…makes sense. This should not have taken 66 replies to solve.


-- Create a global variable to store the target part
local targetPart = nil

-- Create a function to handle mouse events
local function onMouseButton(mouse)
  -- Check if the left mouse button was pressed
  if mouse.button == Enum.UserInputType.MouseButton1 then
    -- Check if the target part is currently set
    if targetPart then
      -- Unset the target part
      targetPart = nil
    else
      -- Get the part that the mouse is currently hovering over
      local target = mouse.target
      -- Check if the target is a valid part
      if target and target:IsA("BasePart") then
        -- Set the target part
        targetPart = target
      end
    end
  end
end

-- Connect the mouse button event to the function
game:GetService("UserInputService").InputBegan:Connect(onMouseButton)

-- Create a function to update the target part's position
local function updatePart()
  -- Check if the target part is set
  if targetPart then
    -- Get the current mouse position
    local mousePosition = game:GetService("UserInputService"):GetMouseLocation()
    -- Set the target part's position to the mouse position
    targetPart.Position = mousePosition
  end
end

-- Run the update function every frame
game:GetService("RunService").RenderStepped:Connect(updatePart)

local mousePosition = game:GetService("UserInputService"):GetMouseLocation()
local camera = game.Workspace.CurrentCamera
local ray = camera:ScreenPointToRay(mousePosition.X, mousePosition.Y)
local hit, position = game.Workspace:FindPartOnRay(ray)

-- Check if the ray hit a part
if hit then
  -- Set the target part's position to the position of the hit
  targetPart.Position = position
else
  -- Set the target part's position to the end of the ray
  targetPart.Position = ray.Origin + ray.Direction * 100
end

try this too if you want to… and dont forget to change directories of the variables. And paste it in a normal script in workspace or serverscriptservice

Thanks, would changing that to the cframe lerp work as I need it smooth also the issue of not being able to stop moving is still there

1 Like

You should tween the position like this:

local hit = mouse.Hit.Position * Vector3.new(1,0,1)
local up = Vector3.new(0,part.Size.Y/2,0)
game.TweenService:Create(part, TweenInfo.new(0.3,Enum.EasingStyle.Quad,Enum.EasingStyle.Out,0,false,0),
	{CFrame = CFrame.new(hit + up)}):Play()

Thanks to blue from me too to end the pain of all

Out is not a valid member of “Enum.EasingStyle”

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = workspace.Parta
local hit = mouse.Hit.Position * Vector3.new(1,0,1)
local up = Vector3.new(0,part.Size.Y/2,0)

local tracking = false
part.ClickDetector.MouseClick:Connect(function()
	if not tracking then
		tracking = true
	end
end)
mouse.Button1Down:Connect(function()
	if tracking then
		tracking = false
	end
end)

mouse.Move:Connect(function()
	if tracking then
		game.TweenService:Create(part, TweenInfo.new(0.3,Enum.EasingStyle.Quad,Enum.EasingStyle.Out,0,false,0),
			{CFrame = CFrame.new(hit + up)}):Play()
	end
end)

try mine too if not working for you bro

Replace with Enum.EasingDirection.Out, was just a typo.

Also, here’s the full script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = workspace.Parta

local tracking = nil -- will be the part your moving
mouse.Button1Down:Connect(function()
	if tracking then 
		tracking = nil
		return
	end
	local hit = mouse.Target
	if hit == part then
		tracking = hit
	end
end)

mouse.Move:Connect(function()
	if tracking then
		local hit = mouse.Hit.Position * Vector3.new(1,0,1)
		local up = Vector3.new(0,tracking.Size.Y/2,0)
		game.TweenService:Create(tracking, TweenInfo.new(0.3,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0),
			{CFrame = CFrame.new(hit + up)}):Play()
	end
end)

This should be the last reply.

Its works amazing but you cant stop it from moving if you click it again

this is the last thing
30303030

Forgot to add a return, now this is the last reply. I update the script, last time. This topic is getting too long for a simple script.

where does the return go?
30303030

Thank you it works amazing thanks

Glad to hear it! You should mark the correct post as the solution for other people to know it was the solution.

78 replies and no solution, even tho issue was solved. Sigh.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.