Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Calling a random function?

Is it possible to call a random function that I made previously? I'm creating an NPC with about 10 skills and animations handled separately at each function, but I don't know how to call it.

This is how I'm trying to do it right now.

function skill1()
	return
end

function skill2()
	return
end

function skill3()
	return
end

local moves = {skill1(), skill2(), skill3()}

while true do
	moves[math.random[1,#moves]]
	wait()
end

But it doesn't work and simultaneously calls it together when I define moves.

3 answers

✓ Accepted answer

You're on the right track. Only a few changes. On line 13:

local moves = {skill1(), skill2(), skill3()}

What you're doing here is running each function and storing its return value in the table. Anytime you include the parenthesis, the VM thinks that you intend to run the function immediately. If you want to store the function, write it without the parenthesis.

local moves = {skill1, skill2, skill3}

By that same token, in order to run the function, you need to include the parenthesis. Looking at line 16; here, you're indexing the function but not calling it. In order to do so change it from:

moves[math.random[1,#moves]]

to

moves[math.random(1,#moves)]()

Also notice that the random() function uses a parenthesis rather than brackets. That has been changed to reflect. So for the final script:

function skill1()
    print("Skill1")
end

function skill2()
    print("Skill2")
end

function skill3()
    print("Skill3")
end

local moves = {skill1, skill2, skill3}

while true do
	moves[math.random(1,#moves)]()
	wait(1)
end

    function attack(number)
    	 if number == 1 then
    		 return
    	elseif number == 2 then
    		return
    	--more here
    	end
    end

while wait() do
	spawn(function() attack(math.random(1, 10)) end)
end

i hope this will help you.

4 comments

0

btw dont use spawn() if you dont want the attacks to stack

0

That does not do anything. Also, please explain your answer and do not simply post code. That's not the point of the site.

0

ok so every ~.03 seconds the function will be spawned with a random argument from 1 to 10, that argument will be used in the main function to see what attack it should make.

0

That is totally not what the point of the question is. The point of the question is calling a random FUNCTION, not a random ARGUMENT. It's called reading the question and not being blind.

You can assign variable names and call the function accordingly:

local FunctionTable = {}
local func1 = function()
	return
end
local func2 = function()
	return
end
table.insert(FunctionTable, func1)
table.insert(FunctionTable, func2)
local RandomFunction = FunctionTable[math.random(1, #FunctionTable)]
coroutine.wrap(RandomFunction)()

Using a table is the way to go here. You can access a random function from the table and call it via coroutine.wrap(), which creates a coroutine from a function and returns a function.

4 comments

0

you shouldnt be using spawn, use coroutine.wrap

0

Fixed that part.

0

Dandystan, that's wrong.

This discussion is preserved as it was. New answers, comments, and votes are closed.