How do I randomize MapNames?

I have a module script called MapNames, as you can guess. It holds the map names and I need to check if a player is not within a elevator and so it waits 10 seconds and then changes a name to something random from MapNames and then well. Randomizes it. It’s really difficult for me to fix this.

Elevator Script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local serverScriptService = game:GetService("ServerScriptService")
local TeleportService = game:GetService("TeleportService")

local safeTeleport = require(serverScriptService.SafeTeleport)

local setDataRE = ReplicatedStorage:WaitForChild("setData")

local movingEvent = ReplicatedStorage:WaitForChild("MovingElevator")
local elevatorEvent = ReplicatedStorage:WaitForChild("Elevator")
local elevator = script.Parent
local prismatic = elevator.Shaft.PrismaticConstraint
local gui = elevator.Screen.SurfaceGui
local mapgui = elevator.MapScreen.SurfaceGui
local config = elevator.Config
local playersWaiting = {}
local countdownRunning = false
local moving = false

local function Setup()
	playersWaiting = {}
	moving = false
	mapgui.MapImage.Image = config.MapData.Texture
	mapgui.MapName.Text = config.MapName.Value
	mapgui.MapName.BackgroundColor3 = config.MapColor.Value
	mapgui.MapMode.BackgroundColor3 = config.MapColor.Value
	mapgui.MapMode.Text = config.MapMode.Value
	gui.Title.Text = #playersWaiting .. "/" .. config.MaxPlayers.Value .. " Players"
	gui.Status.Text = "Waiting..."
end

local function TeleportPlayers()
	local placeId = config:WaitForChild("PlaceID").Value
	local server = TeleportService:ReserveServer(placeId)
	local options = Instance.new("TeleportOptions")
	options.ReservedServerAccessCode = server
	
	for i,v in pairs(playersWaiting) do
		setDataRE:FireClient(v, mapgui.MapName.Text)
	end
	
	TeleportService:SetTeleportSetting("MapData", mapgui.MapName.Text)
	safeTeleport(placeId, playersWaiting, options)
end

local function MoveElevator()
	moving = true
	for i,player in pairs(playersWaiting) do
		movingEvent:FireClient(player)
	end
	gui.Status.Text = "Teleporting Players..."
	prismatic.TargetPosition = -20
	TeleportPlayers()
	script.Parent.Platform.Lower:Play()
	task.wait(5)
	script.Parent.Platform.Rise:Play()
	prismatic.TargetPosition = 0
	task.wait(8)
	Setup()
end

local function RunCountdown()
	countdownRunning = true
	for i=10, 1, -1 do
		gui.Status.Text = "Lowering in... " .. i
		task.wait(1)
		if #playersWaiting < 1 then
			countdownRunning = false
			Setup()
			return
		end
	end
	MoveElevator()
	countdownRunning = false
end

elevator.Entrance.Touched:Connect(function(part)
	local player = Players:GetPlayerFromCharacter(part.Parent)
	local isWaiting = table.find(playersWaiting, player)

	if player and not isWaiting and #playersWaiting < config.MaxPlayers.Value and not moving then
		table.insert(playersWaiting, player)
		mapgui.MapImage.Image = config.MapData.Texture
		mapgui.MapName.Text = config.MapName.Value
		mapgui.MapName.BackgroundColor3 = config.MapColor.Value
		gui.Title.Text = #playersWaiting .. "/" .. config.MaxPlayers.Value .. " Players"
		player.Character.PrimaryPart.CFrame = elevator.TeleportIn.CFrame
		elevatorEvent:FireClient(player, elevator)
		if not countdownRunning then
			RunCountdown()
		end
	end
end)

elevatorEvent.OnServerEvent:Connect(function(player)
	local isWaiting = table.find(playersWaiting, player)
	if isWaiting then
		table.remove(playersWaiting, isWaiting)
	end

	mapgui.MapImage.Image = config.MapData.Texture
	mapgui.MapName.Text = config.MapName.Value
	mapgui.MapName.BackgroundColor3 = config.MapColor.Value
	gui.Title.Text = #playersWaiting .. "/" .. config.MaxPlayers.Value .. " Players"

	if player.Character then
		player.Character.PrimaryPart.CFrame = workspace.TeleportOut.CFrame
	end
end)

Setup()

--[[
while true do
	task.wait(120)
	Setup()
end
]]

Partially done randomize script within the Elevator:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local mapGui = script.Parent.Screen.SurfaceGui
local playerAmount = mapGui.Title
local becamezero = tick()

local MapNames = require(ReplicatedStorage.MapNames)

function randomizeMap()
	local selectedMap = MapNames[math.random(1,  #MapNames)]
	local mapScreen = script.Parent.MapScreen
	script.Parent.Config.MapName.Value = selectedMap
end


task.delay(10, function()
	if tick()-becamezero >= 10 then
		randomizeMap()
	end
end)

playerAmount:GetPropertyChangedSignal("Text"):Connect(function()
	if playerAmount.Text == "0/4 Players" then
		becamezero = tick()
		task.delay(10, function()
			if tick()-becamezero >= 10 then
				randomizeMap()
			end
		end)
	end
end)

I am really puzzled as of now.

The mistake was in the task.delay function. The function was never called because the condition was never met.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local mapGui = script.Parent.Screen.SurfaceGui
local playerAmount = mapGui.Title
local becamezero = tick()

local MapNames = require(ReplicatedStorage.MapNames)

function randomizeMap()
	local selectedMap = MapNames[math.random(1,  #MapNames)]
	local mapScreen = script.Parent.MapScreen
	script.Parent.Config.MapName.Value = selectedMap
end


task.delay(10, function()
	if tick()-becamezero >= 10 then
		randomizeMap()
	end
end)

playerAmount:GetPropertyChangedSignal("Text"):Connect(function()
	if playerAmount.Text == "0/4 Players" then
		becamezero = tick()
		task.delay(10, function()
			if tick()-becamezero >= 10 then
				randomizeMap()
			end
		end)
	end
end)

Elevator Script:

local function TeleportPlayers()
	local placeId = config.PlaceID.Value
	local server = TeleportService:ReserveServer(placeId)
	local options = Instance.new("TeleportOptions")
	options.ReservedServerAccessCode = server
	
	for i,v in pairs(playersWaiting) do
		setDataRE:FireClient(v, mapgui.MapName.Text)
	end
	
	TeleportService:SetTeleportSetting("MapData", mapgui.MapName.Text)
	TeleportService:TeleportToPlaceInstance(placeId, options)
end

local function MoveElevator()
	moving = true
	for i,player in pairs(playersWaiting) do
		movingEvent:FireClient(player)
	end
	gui.Status.Text = "Teleporting Players..."
	prismatic.TargetPosition = -20
	TeleportPlayers()
	script.Parent.Platform.Lower:Play()
	task.wait(5)
	script.Parent.Platform.Rise:Play()
	prismatic.TargetPosition = 0
	task.wait(8)
	Setup()
end

local function RunCountdown()
	countdownRunning = true
	for i=10, 1, -1 do
		gui.Status.Text = "Lowering in... " .. i
		task.wait(1)
		if #playersWaiting < 1 then
			countdownRunning = false
			Setup()
			return
		end
	end
	MoveElevator()
	countdownRunning = false
end

elevator.Entrance.Touched:Connect(function(part)
	local player = Players:GetPlayerFromCharacter(part.Parent)
	local isWaiting = table.find(playersWaiting, player)

	if player and not isWaiting and #playersWaiting < config.MaxPlayers.Value and not moving then
		table.insert(playersWaiting, player)
		mapgui.MapImage.Image = config.MapData.Texture
		mapgui.MapName.Text = config.MapName.Value
		mapgui.MapName.BackgroundColor3 = config.MapColor.Value
		gui.Title.Text = #playersWaiting .. "/" .. config.MaxPlayers.Value .. " Players"
		player.Character.PrimaryPart.CFrame = elevator.TeleportIn.CFrame
		elevatorEvent:FireClient(player, elevator)
		if not countdownRunning then
			RunCountdown()
		end
	end
end)

elevatorEvent.OnServerEvent:Connect(function(player)
	local isWaiting = table.find(playersWaiting, player)
	if isWaiting then
		table.remove(playersWaiting, isWaiting)
	end

	mapgui.MapImage.Image = config.MapData.Texture
	mapgui.MapName.Text = config.MapName.Value
	mapgui.MapName.BackgroundColor3 = config.MapColor.Value
	gui.Title.Text = #playersWaiting .. "/" .. config.MaxPlayers.Value .. " Players"

	if player.Character then
		player.Character.PrimaryPart.CFrame = workspace.TeleportOut.CFrame
	end
end)

Setup()

--[[
while true do
	task.wait(120)
	Setup()
end
]]

The mistake in the code was that the function was not called correctly. The function should have been called like this:

function name(argument)
end

But it was called like this:

function name argument
end

So the function didn’t work correctly.

its simple, you can do this:

local mapNames = {'Map1', 'Map2', 'Map3'}

print(mapNames[math.random(1, #mapNames)])

u already do this sorry

are you setting up the value, in client or in the server?

Im planning to make BIG changes to the script, I plan on making it more cleaner. I will inform you when I am done with this.

I have created a module script that defines mapNames:

local MapNames = {
	
	["Grass Lands"] = {
		["Gamemode"] = "Normal",
		["MapColor"] = Color3.fromRGB(107, 255, 66),
		["MapImage"] = "rbxassetid://11451259367",
		["MaxPlayers"] = 4
	},
	
	["Nuclear Bunker"] = {
		["Gamemode"] = "Normal",
		["MapColor"] = Color3.fromRGB(107, 255, 66),
		["MapImage"] = "rbxassetid://11451259367",
		["MaxPlayers"] = 4
	}
	
}

return MapNames

Just realised I did this before! I also stored it in the server.

I think the problem is that it looks like it does nothing. No errors what so ever after following your script.

ReplicatedStorage.MapNameBeta:20: invalid argument #2 to 'random' (interval is empty) 

nevermind.