Is there a function that fires another function with a set amount?

To clarify is there some sort of function that basically fires a function for a set amount of time while having an argument?

I’m trying to make some automation but I need a function to be called a specific amount of time depending on something. But I’m not sure if there’s something like this actually exists or I’m just plain stupid.

Example of the function I’m trying to go for

function spawnpart()
  local part = Instance.new("Part")
  part.Parent = workspace
  wait()
end

Call(spawnpart(),5) -- the function will be called five times
1 Like

Remove the _G if you are not using them in different scripts.

local spawnpart

spawnpart = true

_G.Call(5, spawnpart)

And to receive it:

_G.Call = function(5, spawnpart)
       if spawnpart ~= nil then
              --code
       end
end
1 Like

Utilising for loops:

function Call(f, amount, ...)
    for iterations = 1, amount do
        f(...)
    end
end

Call(spawnpart, 5)
2 Likes

thanks but how do I set the arguments for the function(spawnpart) because it looks like I couldn’t set it without parentheses.

… is a tuple of arguments. Search for “variadic” functions.

1 Like

There is no built-in function that will fire a function an x number of times. You can make your own, however, like DeveloperBLK did (I don’t recommend using _G at all, however).

Since everyone already gave the baisc amount. I’ll give my own special one.

--!strict
-- MODULE SCRIPT

return function(f, n: number, ...)
	for _ = 1, n do
		f(...)
	end
end
2 Likes

The ... argument of the Call function represents a tuple of a variable amount of arguments that will be passed into the function:

function hi(str)
    print("Hi " .. str)
end

Call(hi, 5, "World")
--[[
Hi World (x5)
--]]
1 Like

thanks I now understand I appreciates everyone help.

Are you trying to look for something similar to spawn()?

_G is only for firing a script to another script. Just use a local function if you are firing in the same script.

_G is usually not recommended to be used due to its:

  • Unreadability: _G.call imo looks gross and often hard to maintain because you usually don’t know where it is accessed and manipulated
  • Unrealiablity: It may take a bit for variables to be accessed by different scripts

Module scripts are a far better alternative


@magicalmariomario A variable amount of arguments should be the last argument:

return function(f, n: number, parallel: boolean, ...)

I recently realized that added the parallel variable is actually repetitive since you can just do call(task.spawn, 5, function() end) instead. So, your script works best.

ex:

--!strict

local function call(f, n: number, ...)
	for _ = 1, n do
		f(...)
	end
end

local func = function()
	print("Hi")
	task.wait(0.25)
end

call(func, 5) -- Has delay
call(task.spawn, 5, func) -- No delay

If you wanted to pass an argument, then you can do (this is pretty advanced though, ngl):

--!strict

local function call(f, n: number, ...)
	for _ = 1, n do
		return f(...)
	end
end

local func = function(text, moreText)
	task.wait(0.25)
	return text, moreText
end

local text, moreText = call(func, 5, "Hello", "Human") -- Has delay
call(task.spawn, 5, func, "Hello", "Human") -- Cannot return values
print(text, moreText)