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

How could i pick random function from an array?

I have tried

random = (input[math.random(1,#input)])

but that isn't what i want to get cause i dont find any way to execute random function that has been choosen

Edit 1 : Part of the table code

input = {moveforward(),movebackwards(),moveleft(),moveright()} 

3 comments

0

Maybe try using a table and if you select one randomly you will use a function connected to that.

0

Please post more of the script. Especially the part with the array

0

edit:- fixed codeblock

1 answer

The roblox wiki says:This function is an interface to the simple pseudo-random generator function rand provided by ANSI C. (No guarantees can be given for its statistical properties.) When called without arguments, returns a uniform pseudo-random real number in the range [0,1). When called with an integer number m, math.random returns a uniform pseudo-random integer in the range [1, m]. When called with two integer numbers m and n, math.random returns a uniform pseudo-random integer in the range [m, n].

This basically, means that math.random is a random generator that fires from 1 to the argument you gave it so lets say our script is from 1 and to 100 it will pick any random number from that number code. And returns a uniform integer basically means it will return a full number.

All you have to do to pick a number and choose a function called math.random

x = math.random(1, 100)
print(x) -- the code should print anywhere from 1 to 100
-- if you want to make it print it once every couple seconds then add a debounce.

I made a simple script for you to understand

for a touched event all you have to do is

local debounce = true
part = script.Parent
part.Touched:Connect(function(hit)
	if debounce then
	if hit.Parent:FindFirstChild("Humanoid")
	       then
		 local x = math.random(1, 10)
		wait(1)
         print(x)
        debounce = false
       end
	end
end)


Note: This will only fire once this is because we need a loop.

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