How would I use TweenService without yielding the script

I want to make an enemy movement system using tweenservice, but when I do it, it puts the entire script on yield until it finishes.

local pathFolder = workspace.PathFolder
local paths = {pathFolder["1"],pathFolder["2"],pathFolder["3"], pathFolder["4"], workspace.End}

local tweenService = game:GetService("TweenService")

local moveInfo = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
local rotateInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)

local function CreateEnemy(enemy) -- the function
	local enemy = enemy:Clone()
	local finished = true

	enemy.Parent = workspace
	enemy.Position = workspace.Start.Position 

	for i=1, #paths do
		repeat wait() until finished == true

		finished = false
		local moveTween = tweenService:Create(enemy, moveInfo, {Position = paths[i].Position})
		local rotateTween = tweenService:Create(enemy, rotateInfo, {CFrame = CFrame.lookAt(enemy.Position, paths[i].Position)})

		moveTween:Play() -- moving the enemy
		rotateTween:Play()

		moveTween.Completed:Connect(function()
			finished = true
		end)
	end
end
	



CreateEnemy(game.ReplicatedStorage.AngryBoy) -- waits untill it is finished tweening through the whole path before going onto the next enemy

CreateEnemy(game.ReplicatedStorage.AngryBoy)

	

	




Any help would be appreciated, I’ve tried using coroutines but it still puts the script on yield, so I’m not sure what to do.

Instead of:

Try using coroutine.wrap()() which shouldn’t yield

coroutine.wrap(CreateEnemy)(game.ReplicatedStorage.AngryBoy) -- waits untill it is finished tweening through the whole path before going onto the next enemy

coroutine.wrap(CreateEnemy)(game.ReplicatedStorage.AngryBoy)

Edit: Try again. Fixed spelling error

1 Like

Thanks, this worked perfectly! I tried using this before but I think I did it incorrectly. :grin:

1 Like

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