I know i'm supposed to use coroutines but idk how

Hey all,

In this code, the tween was previously happening on the client, but now that I’ve moved it to the server script it has a bug, because now the stuff that I need to run at the same time (a gui tween, and making a part invisible) cant happen because they are running at the same time.

of course the solution is a coroutine but I am not sure how to implement this.

		elseif Type == "Cloak" then
			coroutine.resume(ReloadCloak(CloakDuration, CloakReload))
			--as you can see, right here i tried to implement the coroutine, but to no avail.
				if team == red then
					for _, bruh in pairs(teams['Red']:GetPlayers()) do
						local children = bodykit:GetDescendants()
						--the rest is irrelevant...

ReloadCloak()

function ReloadCloak(CloakDuration, CloakReload)
		if CloakEnabled == false then
			newgui.Maxcloak.Cloak.Text = "Cloaking..."
			local CloakTween = newgui.Maxcloak.Cloakbar:TweenSize(
				UDim2.fromScale(0,1),
				Enum.EasingDirection.In,
				Enum.EasingStyle.Linear,
				CloakDuration,
				false)
			wait(CloakDuration)
			newgui.Maxcloak.Cloak.Text = "Reloading..."
			local ReloadTween = newgui.Maxcloak.Cloakbar:TweenSize(
				UDim2.fromScale(1,1),
				Enum.EasingDirection.In,
				Enum.EasingStyle.Linear,
				CloakReload,
				false)
			wait(CloakReload)
			newgui.Maxcloak.Cloak.Text = "Cloak"
			CloakEnabled = true
		end
	end

Anyone able to fix this?
Thanks!

To create a coroutine do this:

local function CodeYouWantToRun(...)
	print("thread spawned with args:", ...)
end
local thread = coroutine.create(CodeYouWantToRun)

To run the coroutine do this:

coroutine.resume(thread, 1, 2, 3)

Docs:

1 Like