How do i make this function synchronous?

Hello! Ive made this function and whenever i call it it immediately goes onto the next thing in the code.

		local function Mainanimation(AnimName, looped, bpm)
		
		Loop = AnimName
		local Timeing = 0
		local CurrentAnimation = 1
		local CurrentSkin = "Default"

		local currentAnimation = animations[CurrentSkin] or nil

		if currentAnimation ~= nil then
			local animation = currentAnimation[AnimName] or nil
			if animation ~= nil then
				local Runstepped
				Runstepped = runService.RenderStepped:Connect(function(timeSince)
					Timeing += timeSince

					if Loop ~= AnimName then
						Runstepped:Disconnect()
						--print("Err")
						return true
					end
					
					if animation[CurrentAnimation] ~= nil then
						
						offset = 60/bpm
						if offset <= Timeing then
							PlayAnimation(animation[CurrentAnimation]['id'])
							CurrentAnimation += 1
							Timeing = 0
						end
					else
						Runstepped:Disconnect()
						--print("Done", Timeing)
						return true
					end

				end)
			end
		end
	end
	local function Giveanimation(AnimName, looped, bpm)
		
		
		if looped and Loop ~= AnimName then
			Loop = AnimName
			while Loop == AnimName do
				local ret
					ret = Mainanimation(AnimName, looped, bpm)
				task.wait(bpm/60)
			end
		elseif Loop ~= AnimName then
			Loop = AnimName
			Mainanimation(AnimName, Loop, bpm)
		end
	end

What can i do to make this function synchronous? im thinking it probably has something to do with the renderstepped?

1 Like

It’s related to RenderStepped you’re right, to make the Mainanimation function synchronous you can convert it into a coroutine

The issue with the renderstepped : Basically, It’s an asynchronous event that continuously fires during each frame update your code moves onto the next line immediately after connecting the event, resulting in the function not behaving synchronously as you intend

local function Giveanimation(AnimName, looped, bpm)
	local CInstance = Mainanimation(AnimName, looped, bpm)
	if CInstance then
		if looped and Loop ~= AnimName then
			Loop = AnimName
			while Loop == AnimName do
				coroutine.resume(CInstance)
				task.wait(bpm / 60)
			end
		elseif Loop ~= AnimName then
			Loop = AnimName
			coroutine.resume(CInstance)
		end
	end
end
1 Like

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