Make a function non-yielding?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make this function I created to be non-yielding aka run alongside another function.

  2. What is the issue? Include screenshots / videos if possible!

function dreamer()
	local players = teams.Kid:GetPlayers()
	while dreamTime > 0 do
		dreamTime = dreamTime - 1
		wait(1)
	end
	for i, v in pairs(players) do
		v.Character.HumanoidRootPart.CFrame = currentmap:FindFirstChild(chosenmap.Value).Dream_End.CFrame
	end
end
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve done many google and dev hub searches and can only find the opposite of what I’m looking for.
1 Like

Just use spawn()
Here’s an article that explains it.
https://developer.roblox.com/en-us/articles/Threading-Code

Use

coroutine.wrap(function() —[[your code]] end)()
function dreamer()
	local players = teams.Kid:GetPlayers()
          coroutine.resume(coroutine.create(function()
while dreamTime > 0 do
		dreamTime = dreamTime - 1
		wait(1)
	end
end)
	
	for i, v in pairs(players) do
		v.Character.HumanoidRootPart.CFrame = currentmap:FindFirstChild(chosenmap.Value).Dream_End.CFrame
	end
end

If my calculations are correct this should work

So instead of calling the function with

dreamer()

I would call it with

spawn(dreamer)

Yes that would work. But remember spawn() is delayed and the delay can build up

Use coroutine.wrap(dreamer)()

That’s the best solution. No delay.

1 Like

I will try all of your solution to see which one works the best!

For some reason the teleporting part (below) doesnt work anymore.

for i, v in pairs(players) do
		v.Character.HumanoidRootPart.CFrame = currentmap:FindFirstChild(chosenmap.Value).Dream_End.CFrame
end

Edit: It works fine with spawn()

1 Like

The difference in using coroutine.wrap is that it runs the code immediately when called and only continues after hitting a yield within the function. Spawn will schedule the function to start at some time in the future, possibly with a yield. It runs the code following the spawn call before the code called with spawn. Using coroutine.wrap is ideal in most cases.

2 Likes

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