Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How could i pick random function from an array?

Asked by 4 years ago
Edited 4 years ago

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()} 
0
Maybe try using a table and if you select one randomly you will use a function connected to that. kingblaze_1000 359 — 4y
0
Please post more of the script. Especially the part with the array Infocus 144 — 4y
0
edit:- fixed codeblock User#5423 17 — 4y

1 answer

Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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.

Ad

Answer this question