How to not yield the script while executing a mainmodule function?

I want to make a gui that tweens in and out when the player is teleporting, but I don’t know/know how to make it so it won’t yield the script.

MM Code:

function Duckss:LoadUI(Player)
	local Gui = Player.PlayerGui.MainDuck.LoadingFrame
	local Info = {
		UDim2.new(0, 0, 0, 0),
		Enum.EasingDirection.Out,
		Enum.EasingStyle.Sine,
		1
	}
	
	Gui:TweenPosition(Info)
end

function Duckss:RemoveUI(Player)
	local Gui = Player.PlayerGui.MainDuck.LoadingFrame
	
	local Info = {
		UDim2.new(0, 0, -1, 0),
		Enum.EasingDirection.In,
		Enum.EasingStyle.Sine,
		0.75
	}
	
	Gui:TweenPosition(Info)
end

Script:

local function onClick(Player)
	if Player.Character and On then
		On = false
		local Character = Player.Character
		local HumanoidRootPart = Character.HumanoidRootPart
		
		Character.AAC.Client.Disabled = true
		wait(0.9)
		HumanoidRootPart.Position = Vector3.new(script.Parent.Parent.Dominus.Position.X, script.Parent.Parent.Dominus.Position.Y + 6, script.Parent.Parent.Dominus.Position.Z)
		wait(0.9)
		Character.AAC.Client.Disabled = false
		On = true
	end
end

script.Parent.ClickDetector.MouseClick:Connect(onClick)
1 Like

Take a look at coroutines: coroutine | Roblox Creator Documentation

1 Like

Would with work with module scripts? Because it says it only works within the same script.

Yeah, I think it should, since when you require a module script, the code from the module script is executed in the script that required it.

So it works, the tweening part does, but gives me an error. If I wrap it in a pcall it still won’t run.

Error:

missing argument #1 to 'create' (function expected) 

I’m pretty sure this is because the function isn’t in the script.

You can spawn a new thread with the “spawn” function, which I find easier to use than coroutines.
Example:

spawn(function()
     Duckss:LoadUI(Player)
end

You can use the spawn function in any script and it will execute synchronously (won’t disrupt any other script).

I was doing something similar, but putting the LoadUI function inside of the spawn() brackets.
Thank you!

1 Like

task.spawn() or coroutine.wrap()/coroutine.create() should be used over spawn()
this gist explains why:
https://eryn.io/gist/3db84579866c099cdd5bb2ff37947cec