Help making part follow mouse

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    The video.

  1. What is the issue? Include screenshots / videos if possible!
    The part keeps appearing under the map.

(Sorry for bad quality my PC sucks)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried looking at dev-forum posts but it didn’t help.

Code:

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local held = false
local p = workspace:WaitForChild("Part")
local start = p.CFrame

function update()
	local pos = mouse.Hit.Position
	local distance = (start.Position - pos).magnitude 

	if distance > 1000 then
		distance = 1000
	end

	p.Size = Vector3.new(p.Size.X, p.Size.Y, distance)
	p.CFrame = CFrame.new(Vector3.new(start.Position.X, .5, start.Position.Z), Vector3.new(pos.X, pos.Y, pos.Z)) * CFrame.new(0, 0, -distance / 2)
	
end

tool.Activated:Connect(function()
	
	held = true
	
	while held == true do
		wait()
		
		update()
		
	end
	
end)

tool.Deactivated:Connect(function()
	held = false
end)
3 Likes

It seems like the code is alright. Are you trying to make the part originate from the character to the mouse?

Yeah. I’m trying to make it like the video, but it only appears under the map for some reason.

I’m pretty sure that’s because the start position in under the map. Where exactly is the start part?

It’s the red part in the start of the video, and yeah its above the map.

Here, let’s try changing the “.5” to something different.

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local held = false
local p = workspace:WaitForChild("Part")
local start = p.CFrame

function update()
	local pos = mouse.Hit.Position
	local distance = (start.Position - pos).magnitude 

	if distance > 1000 then
		distance = 1000
	end

	p.Size = Vector3.new(p.Size.X, p.Size.Y, distance)
	p.CFrame = CFrame.new(Vector3.new(start.Position.X, 25, start.Position.Z), Vector3.new(pos.X, pos.Y, pos.Z)) * CFrame.new(0, 0, -distance / 2)
	
end

tool.Activated:Connect(function()
	
	held = true
	
	while held == true do
		wait()
		
		update()
		
	end
	
end)

tool.Deactivated:Connect(function()
	held = false
end)

Show me how it looks now.

2 Likes

I changed the number and it works now, thanks!