How do I skip waiting for a function

I’m trying to skip waiting for a function. Is this possible?

local temp = script.Parent.Template

local function create(text, deleteOthers, timee)
	local clone = temp:Clone()
	clone.Parent = script.Parent
	clone.Name = text:split(" ")[1]
	clone.Text = text
	if deleteOthers == true then
		for i, v in pairs(script.Parent:GetChildren()) do
			if v:IsA("TextLabel") then
				if v.Name == "Template" then return end
				v:Delete()
			end
		end
	end
	wait(timee)
	clone:Destroy()
end


create("Test1",false,1)
create("Test2", true, 1)

Any help would be appreciated!

1 Like

You can skip waiting by using coroutines/task.spawn.

local function create(text, deleteOthers, timee)
	local clone = temp:Clone()
	clone.Parent = script.Parent
	clone.Name = text:split(" ")[1]
	clone.Text = text
	if deleteOthers == true then
		for i, v in pairs(script.Parent:GetChildren()) do
			if v:IsA("TextLabel") then
				if v.Name == "Template" then return end
				v:Delete()
			end
		end
	end
task.spawn(function()
	wait(timee)
	clone:Destroy()
end)
end
2 Likes

You could set wait to zero.

wait(0)

end

Adding onto @dthecoolest, here is an example using coroutines.

local temp = script.Parent.Template
local function create(text, deleteOthers, timee)
	local clone = temp:Clone()
	clone.Parent = script.Parent
	clone.Name = text:split(" ")[1]
	clone.Text = text
	if deleteOthers == true then
		for i, v in pairs(script.Parent:GetChildren()) do
			if v:IsA("TextLabel") then
				if v.Name == "Template" then return end
				v:Delete()
			end
		end
	end
    coroutine.wrap(function()
	   wait(timee)
	   clone:Destroy()
    end)() -- Notice how there needs to be parenthesis here, this is because coroutine.wrap() returns a function in a new thread and does not immediately execute it like task.spawn()
end

Coroutines also have some other cool features that you should totally check out! Here’s the link to the API reference if you’re interested: coroutine | Roblox Creator Documentation

You can also read up on this post to see specifics about the differences between the two.

2 Likes