How to use task.spawn() to call a premade function

Whenever I run this line of code:

local function typewriter(textObj, statement)
	textObj.Text = ""
	for i = 1, #statement, 1 do
		textObj.Text = string.sub(statement, 1, i)
		wait(0.05)
	end
end

task.spawn(typewriter(cstext, "Ciao!"))

I get this error:
image

I tried looking at the documentation for spawn, but I didn’t understand what it was saying. I feel like this isn’t a hard problem to solve, but thanks for informing me anyways.

You should only pass the function itself, then after that send the arguments that will be used in the function:

task.spawn(typewriter, cstext, "Ciao!") --> equal to typewriter(cstext, "Ciao!")
local function typewriter(textObj, statement)
	textObj.Text = statement
	label.MaxVisibleGraphemes = 0
	for i = 1, #statement do
		label.MaxVisibleGraphemes = i
		task.wait(0.05)
	end
end

task.spawn(typewriter, cstext, "Ciao!")
1 Like

It’s important to understand how function arguments work. In this scenario, you’re not providing a function object, rather, you’re attempting to execute a function call within the arguments of another function call.

task.spawn(typewriter)(cstext, "Ciao!")

it should work

you can also use coroutine

Although I haven’t seen or tried the solution @napastnikiek1 mentioned, all the suggestions on here should work to my knowledge. And there’s another way to do this too:

task.spawn(function()
	typewriter(cstext, "Caio!")
end)

Any of these should work.

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