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

How do I randomize functions?

Asked by 5 years ago

I am trying to call the functions randomly. But I do not know where to start. Any tips on how to begin? I have tried using math.random but no success.

local paper = game.ReplicatedStorage.Spawnables.Paper
local folder = game.ReplicatedStorage.Spawnables

-- paper spawnings
    function paperSpawn()
    local spawnPaper = paper:Clone()
    spawnPaper.Parent = workspace
    local xpos = math.random(-100, 100)
    local ypos = 0.5
    local zpos = math.random(-100, 100)
    spawnPaper.Position = Vector3.new(xpos, ypos, zpos)
    end
    function voidSpawn()
    local spawnPaper = folder.VoidPaper:Clone()
    spawnPaper.Parent = workspace
    local xpos = math.random(-100, 100)
    local ypos = 0.5
    local zpos = math.random(-100, 100)
    spawnPaper.Position = Vector3.new(xpos, ypos, zpos)
    end
    function redPaper()
    local spawnPaper = folder.RedPaper:Clone()
    spawnPaper.Parent = workspace
    local xpos = math.random(-100, 100)
    local ypos = 0.5
    local zpos = math.random(-100, 100)
    spawnPaper.Position = Vector3.new(xpos, ypos, zpos)
    end
    function LightPaper()
    local spawnPaper = folder.LightPaper:Clone()
    spawnPaper.Parent = workspace
    local xpos = math.random(-100, 100)
    local ypos = 0.5
    local zpos = math.random(-100, 100)
    spawnPaper.Position = Vector3.new(xpos, ypos, zpos)
    end
    function GoldPaper()
    local spawnPaper = folder.GoldPaper:Clone()
    spawnPaper.Parent = workspace
    local xpos = math.random(-100, 100)
    local ypos = 0.5
    local zpos = math.random(-100, 100)
    spawnPaper.Position = Vector3.new(xpos, ypos, zpos)
    end
    function DiamondPaper()
    local spawnPaper = folder.DiamondPaper:Clone()
    spawnPaper.Parent = workspace
    local xpos = math.random(-100, 100)
    local ypos = 0.5
    local zpos = math.random(-100, 100)
    spawnPaper.Position = Vector3.new(xpos, ypos, zpos)
    end
    function EmeraldPaper()
    local spawnPaper = folder.EmeraldPaper:Clone()
    spawnPaper.Parent = workspace
    local xpos = math.random(-100, 100)
    local ypos = 0.5
    local zpos = math.random(-100, 100)
    spawnPaper.Position = Vector3.new(xpos, ypos, zpos)
    end
    function PurplePaper()
    local spawnPaper = folder.PurplePaper:Clone()
    spawnPaper.Parent = workspace
    local xpos = math.random(-100, 100)
    local ypos = 0.5
    local zpos = math.random(-100, 100)
    spawnPaper.Position = Vector3.new(xpos, ypos, zpos)
    end
-- randomizer

1 answer

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago

put all your functions into a table:

myFunctions = {paperSpawn, voidSpawn, redPaper, LightPaper, GoldPaper,DiamondPaper,EmeraldPaper,PurplePaper}

and then use math.random() to get a random functionL

randomFunction = myFunctions[math.random(1, #myFunctions)]

--calls random function
randomFunction()
Ad

Answer this question