Part Only Moves On First Click?

I want to make it so the part moves wherever the mouse is clicked. Currently, the part only moves on the first click, after that, it just doesn’t work. Here is my script, could you help me and see why it doesn’t move after I click a 2nd time?

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	if game.Workspace.Part:FindFirstChild('BodyPosition') == nil then
		local finalPosition = mouse.Hit.Position
		local bodyPos = Instance.new("BodyPosition", game.Workspace.Part)
		bodyPos.Position = finalPosition
	else
		game.Workspace.Part.BodyPosition:Destroy()
		local finalPosition = mouse.Hit.Position
		local bodyPos = Instance.new("BodyPosition", game.Workspace.Part)
		bodyPos.Position = finalPosition
	end
end)

Inside your else statement, the 2nd to last line creates a new BodyPosition instance which in your first if statement you check if it doesn’t exist.

That’s why I use :Destroy()

Am I misunderstanding something?

Well, you did destroy the BodyPosition instance, but then you immedeatly recreateed it, so any further clicks would just run the else statement over and over because it’s destroying then re-creating.

Deleting and re-creating the BodyPosition Instance may not be the best solution either, instead you could add another variable

Try something like…

local player = game.Players.LocalPlayer
local bodyPos
local mouse = player:GetMouse()
local moving = false
mouse.Button1Down:Connect(function()
	if not moving then
        moving = true
		local finalPosition = mouse.Hit.Position
		bodyPos = Instance.new("BodyPosition", game.Workspace.Part)
		bodyPos.Position = finalPosition
	else
		local finalPosition = mouse.Hit.Position
		bodyPos.Position = finalPosition
        moving = false
	end
end)

Yes, then destroying and recreating wouldn’t exactly be the problem. Since, before the script tells the part to move to the mouse position, it firsts creates a BodyPosition. So what would be the inherent issue for it not moving?

Edit: it moves if i spam click it?

Have you tried printing the finalPosition in both the if and else statment to see if it’s giving different positions?

It prints different coordinates; yet it stays in the same position. After a few seconds, when you click it moves to the different mouse position. Why does it take a few seconds for it to work?

How much force are you applying in your BodyPosition?

I would look at the velocity on the part, is your velocity being updated? If you’re velocity is changing but not by much try adjusting the force of the BodyPosition.

I personally don’t use BodyPosition because it’s deprecated, instead I’d utilize AlignPosition to do this which would require the use of attachments.

AlignPosition would work better for RTS, do you think?

Just change the Parts Position instead? Why do you even use a BodyPosition? The Part contains a Position Property. Also you didnt mention a Velocity so there is absolutely no difference if you do this.

Edit: If you still want a Animation i suggest using TweenService.