Functions can't execute at the same time

image
I have a module script which upon a function being ran, it creates a notification for the local player to see. However, if you execute 2 different things at the same time, the next function waits until the first one is done to execute, which is undesired. Does anyone know why?

This is the module script.

local Notify = {}

function Notify.CreateNewLabel(text, color)
	local label = script.Parent.Preset:Clone()
	local stroke = label:FindFirstChildWhichIsA("UIStroke")
	label.Name = "Active"
	script.Parent.Click:Play()
	label.Parent = script.Parent.Text
	label.Text = text
	label.TextColor3 = color
	for count = 20, 0, -1 do
		label.TextTransparency -= 0.1
		stroke.Transparency -= 0.1
		task.wait(0.05)
	end
	task.wait(3.5)
	for count2 = 20, 0, -1 do
		label.TextTransparency += 0.1
		stroke.Transparency += 0.1
		task.wait(0.05)
	end
	task.wait(0.5)
	label:Destroy()
end

return Notify
1 Like

When you call a yeilding (wait) function everything below it will wait for it to finish. You could use task.defer to not wait on the function like so

task.defer(Notify.CreateNewLabel, "Hello", color)
task.defer(Notify.CreateNewLabel, "Don't do that", color)

If you want your label to never be waited on you can modify your module like so and call it as you would normally.

function Notify.CreateNewLabel(text, color)
	local label = script.Parent.Preset:Clone()
	local stroke = label:FindFirstChildWhichIsA("UIStroke")
	label.Name = "Active"
	script.Parent.Click:Play()
	label.Parent = script.Parent.Text
	label.Text = text
	label.TextColor3 = color
	task.defer(function()
		for count = 20, 0, -1 do
			label.TextTransparency -= 0.1
			stroke.Transparency -= 0.1
			task.wait(0.05)
		end
		task.wait(3.5)
		for count2 = 20, 0, -1 do
			label.TextTransparency += 0.1
			stroke.Transparency += 0.1
			task.wait(0.05)
		end
		task.wait(0.5)
		label:Destroy()
	end)
end
1 Like

You have for loops in your function, so it’ll wait until it’s done. To bypass this you can use:

coroutine.wrap(function()
 Notify.CreateNewLabel("Hello", color)
end)()
1 Like

You’d need to wrap the CreateNewLabel in a task.spawn call so it runs independantly.

local Notify = {}

function Notify.CreateNewLabel(text, color)
	task.spawn(function()
		local label = script.Parent.Preset:Clone()
		local stroke = label:FindFirstChildWhichIsA("UIStroke")
		label.Name = "Active"
		script.Parent.Click:Play()
		label.Parent = script.Parent.Text
		label.Text = text
		label.TextColor3 = color
		for count = 20, 0, -1 do
			label.TextTransparency -= 0.1
			stroke.Transparency -= 0.1
			task.wait(0.05)
		end
		task.wait(3.5)
		for count2 = 20, 0, -1 do
			label.TextTransparency += 0.1
			stroke.Transparency += 0.1
			task.wait(0.05)
		end
		task.wait(0.5)
		label:Destroy()
	end)
end

return Notify
1 Like

Thanks to everyone that replied, all the methods work.

1 Like

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