My Moving Part Script only fires the first time in a loop. How can I fix this?

Hi! My code gets no errors, but when I fire it, it tweens the first time then never continues again. I just learned tweenservice so I might be wrong about something. Thanks!

local tweenService = game:GetService('TweenService')

local part = script.Parent
while true do
	wait(1)
local goal = {}
if part.Position == part.Parent.TargetOne.Position then
	goal.Position = script.Parent.Parent.TargetTwo.Position
	local tweenInfo = TweenInfo.new(1)
	local tween = tweenService:Create(part, tweenInfo, goal)
	tween:Play()
elseif part.Position == part.Parent.TargetOne.Position then
		goal.Position = script.Parent.Parent.TargetOne.Position
	local tweenInfo = TweenInfo.new(1)
	local tween = tweenService:Create(part, tweenInfo, goal)
	tween:Play()
else
			goal.Position = script.Parent.Parent.TargetOne.Position
	local tweenInfo = TweenInfo.new(1)
	local tween = tweenService:Create(part, tweenInfo, goal)
	tween:Play()
	
end
end
1 Like

Try this one mate

local part = script.Parent
while true do
	wait(1)
local goal = {}
if part.Position == part.Parent.TargetOne.Position then
	goal.Position = script.Parent.Parent.TargetTwo.Position
	local tweenInfo = TweenInfo.new(1)
	local tween = tweenService:Create(part, tweenInfo, goal)
	tween:Play()
elseif part.Position == part.Parent.TargetTwo.Position then
		goal.Position = script.Parent.Parent.TargetOne.Position
	local tweenInfo = TweenInfo.new(1)
	local tween = tweenService:Create(part, tweenInfo, goal)
	tween:Play()
else
			goal.Position = script.Parent.Parent.TargetOne.Position
	local tweenInfo = TweenInfo.new(1)
	local tween = tweenService:Create(part, tweenInfo, goal)
	tween:Play()
	
end
end

The same thing happens, I’m not sure what the error is.

I personally never use the position to do these kind of things, try using a normal variable instead.
My guess is that the position is 100% equal to the original part’s position.

local part = script.Parent
local CurrentPosition = part.Parent.TargetOne -- start
while true do
	wait(1)
	local goal = {}
	if CurrentPosition == part.Parent.TargetOne then
		goal.Position = script.Parent.Parent.TargetTwo.Position
		local tweenInfo = TweenInfo.new(1)
		local tween = tweenService:Create(part, tweenInfo, goal)
		tween:Play()
		CurrentPosition = goal
	elseif CurrentPosition == part.Parent.TargetTwo then
		goal.Position = script.Parent.Parent.TargetOne.Position
		local tweenInfo = TweenInfo.new(1)
		local tween = tweenService:Create(part, tweenInfo, goal)
		tween:Play()
		CurrentPosition = goal
	else
		goal.Position = script.Parent.Parent.TargetOne.Position
		local tweenInfo = TweenInfo.new(1)
		local tween = tweenService:Create(part, tweenInfo, goal)
		tween:Play()
		CurrentPosition = goal
	end
end

Not a fix, but more of an efficiency thing,
you can simplify your loop from

while true do
wait(1)
end

to

while wait(1) do
end

It will have the exact same effect.

1 Like

P R E O P T O M I Z A T I O N
I S
A
S I N

The reason its not working is because youre checking for positions. You shouldnt rely on two positions being exactly the same because of floating point and a lot of other random stuff that might offset the position between two objects.

What you can do is save the last target youve tweened to or check the magnitude of two positions.

8 Likes

I’m gonna get that on a shirt lmao

3 Likes

I’ll take one.

1 Like

Same.

1 Like

I may or may not make one later today.

1 Like