How would i choose a random game from a table?

Hello, i’m making a minigame game, and i want to play a completely random game.

local games = {
	swordfight = {
		Position = Vector3.new(),
		Tool = game.ReplicatedStorage.Tools.ClassicSword
	}
	
}

How would i choose a random game from the table, and then grab the position and the tool?

1 Like
local game = games[math.random(1, #games)]
local pos = game.Position
local tool = game.Tool


this is my code
image
this is the table
image
Why am i recieving the error?

1 Like

Nevermind - you can’t make a variable called game because that’s a default variable set by Roblox.
Wait, it still didn’t work, I’ll try to find a way to fix it

huh…
0
image

I noticed that if you define the game table before hand, and then put it in the table afterwards it works:

local SwordFight = {
	Position = Vector3.new(0, 0, 0);
	Tool = "Sword";
}

local games = {
	SwordFight;
}


local game = games[math.random(1, #games)]
local pos = game.Position
local tool = game.Tool

May be a studio bug, I’m not sure - but this should work.

do you know any way i could add a function into the table? so i can run like a “main” when a game plays

Yeah, do:

local TableName = {}

function TableName.FuncName()
    -- Code
end

Then to call it:

TableName.FuncName()

You can also use module scripts for this if you want to.

This worked, and yeah that is what i’m doing. creating a game-module for my game