Asynchronous functions

So I have a script that calls a function, but I have a wait function inside the function. Because of this the rest of the script won’t run until the function is done. I’m just wondering if there is a way to make functions async so this won’t happen.

--Inside the function-------
local function tween()
local info = TweenInfo.new(
		0.2,
		Enum.EasingStyle.Quad,
		Enum.EasingDirection.InOut)
	local goal = {}
	goal.Size = Vector3.new(30,1024,30)
	goal.Transparency = 0.6
	script["ES_Hit Impact Blast - SFX Producer"]:Play()
	local tween = TweenService:Create(circle,info,goal)
	tween:Play()
	local tween2 = TweenService:Create(circle2,info,{Size = Vector3.new(30,0.5,30)})
	tween2:Play()
	touch = false
	wait(2)
	local info2 = TweenInfo.new(1)
	local tween = TweenService:Create(circle,info2,{Transparency = 1})
	tween:Play()
	local tween2 = TweenService:Create(circle2,info2,{Transparency = 1})
	tween2:Play()
	tween2.Completed:Wait()
	circle:Destroy()
	circle2:Destroy()
end
--Outside the function-------

tween(player)
task.wait(0.25)
print("server")

If I can remember correctly, using task.wait() should fix the issue.

You can replace the end of your code with this!

--Outside the function-------

spawn(function()
	tween(player)
end)
task.wait(0.25)
print("server")

A spawn function will cause the following function to not yield the script & will continue running regardless of the function having to wait.

7 Likes

I would personally use Promises. They are asynchronous execution made easy.

3 Likes

Wow, this is amazing. How did I now know this??? lol

1 Like

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