Random Event Round System

So I have created a round System of some sort and I want to know what the best way to chose and do
a random event would be

local IntermissionTime = 4

local MinPlayers = 1

local InGameTable = {}
local InLobbyTable = {}


local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")


local Lobby = game.Workspace.Baseplate

local ChosenMap
local Maps = ServerStorage.Maps:GetChildren()
local function RandomMap()
	local RanMap = Maps[math.random(1,#Maps)]:Clone()
	RanMap.Parent = workspace
	print(RanMap)
	ChosenMap = RanMap
end

Players.PlayerAdded:Connect(function(player)
	table.insert(InLobbyTable,player.UserId)
	local Character = player.Character or player.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild("Humanoid")
	Humanoid.Died:Connect(function()
		table.remove(InGameTable,table.find(InGameTable,player.UserId))
		table.insert(InLobbyTable,player.UserId)
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	table.remove(InLobbyTable,table.find(InLobbyTable,player.UserId))
	local CouldBeINGame = table.find(InGameTable,player.UserId)
	if CouldBeINGame then
		table.remove(InGameTable,CouldBeINGame)
	end
end)

local function CheckForPlayers()
	if #Players:GetPlayers() < MinPlayers then
		warn("Not Enough Players")
		repeat wait(1) until #Players:GetPlayers() >= MinPlayers
	end
end

local function StartGame()
	RandomMap()
	
	for i,plr in pairs(InLobbyTable) do
		local player = Players:GetPlayerByUserId(plr)
		
		local Character = player.Character
		local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
		if HumanoidRootPart then
			local Spawns = ChosenMap.Spawns:GetChildren()
			local RandomSpawn = Spawns[math.random(1,#Spawns)]
			HumanoidRootPart.CFrame = RandomSpawn.CFrame
			table.remove(InLobbyTable,table.find(InLobbyTable,plr))
			table.insert(InGameTable,plr)
			print("Payer Name: "..player.Name)
			warn("Game Started")
			print(InLobbyTable)
			print(InGameTable)
		end
	
	end
	ChosenMap.Parent = workspace
	
	
	
	
	
end

local function EndGame()
	
	for i,plr in pairs(InGameTable) do
		local player = Players:GetPlayerByUserId(plr)
		print("Player Name: "..player.Name)
		local Character = player.Character
		local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
		if HumanoidRootPart then
			HumanoidRootPart.CFrame = Lobby.SpawnLocation.CFrame + Vector3.new(0,5,0)
			table.remove(InGameTable,table.find(InGameTable,plr))
			table.insert(InLobbyTable,plr)
			print("Payer Name: "..player.Name)
			warn("Game Ended")
			print(InGameTable)
			print(InLobbyTable)
		end

	end
	
	ChosenMap.Parent = nil
	

	
	
	
	
	
	
	
end



while true do
	CheckForPlayers()
	for i = IntermissionTime,1,-1 do
		print(i)
		CheckForPlayers()
		
		task.wait(1)
	end
	
	StartGame()
	
	
	
	for i = RoundTime,1,-1 do
		print(i)

		task.wait(1)
		
		if #InGameTable == 1 then
			local Winner = Players:GetPlayerByUserId(InGameTable[1])
			print(Winner.Name.." has won")
			Winner.leaderstats.Dubs.Value = Winner.leaderstats.Dubs.Value +1
			task.wait(1)
			break
		end
		if #InGameTable == 0 then
			warn("Everyone Lost")
			task.wait(1)
			break
		end
	end	
	
	EndGame()
	
	
end```
1 Like

By “random event” you mean just something random that happens in the middle of a round, correct?

If so, how specifically?

  • Will multiple events happen?
  • How long will these events be?
1 Like

yes about id hope 3
and random durration deppending on the event

If these events arent too long, simply just:

  • at the start of the round get how long the round will last
  • divide by 3
  • wait however long your result is
  • do an event
  • repeat 2 more times

Ye but i want the best way to chose a random event/function

Put functions into a table WITHOUT the parenthesis or else itll call them

function func1()
--whatever
end
function func2()
--also whatever
end
events = {func1, func2}

Call a random function by getting a random index and calling it

local Choice = math.random(1, #events)
events[Choice]()

Sorry for the long delay, this should work

2 Likes

tysm this was helpful well needed

If you dodnt mind I have another question on how i would go about creating different gamemodes

I took a nap, lol
But what do you mean by gamemodes

  • A different one is picked after each round
  • Players vote for it

I got my question answered but ty anyways

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.