I’m making a game, but the issue is that the game requires me to make a game loop which I’m not very experienced in making. I’ve been scripting for a bit but I haven’t really gotten used to using the three types of scripts (Module Scripts, Server Scripts, and Local Scripts), which makes it harder for me to think about how I want to optimize my code.
The loop will consist of different game modes, team modes (which is the mode that accounts for the amount of players on each team), and will let the players vote for a randomized map that cannot be the same as any other selection button.
Things I’m not sure about
How I’m going to implement the game modes and team modes
How I’m going to randomize the maps while also making sure there not the same
How I’m supposed to optimize all of the scripts
Which scripts I should use and how I should go about using them
How I’m supposed to return the players voted option (GUI)
The loop will then later track every player, what team there on, and each game mode will track the player’s progress throughout the game. After the time is up, the game will end and the team with x will win.
you can use the local script if you want to do something specific to the player you doing it to.
if you want to make something to all the players (the server) you can use server script.
if you want a script that you can run once and must return exactly one value and you can access it from another server script or local script by require then you can use module script
so put this thing in server script
if you want to make the maps & rounds system you can do:
It’s the easiest I could do:
local Players = game:GetService("Players")
local round = 0
local set = {
start_time = 15, -- this is how much time you want to wait after one game finished and you want to start another one.
game_time = 60, -- this is how much time you want the game will be.
}
local maps = {
["1"] = 0,
["2"] = 0,
["3"] = 0,
-- if you have more maps then you can add more numbers.
}
local map1button = the_button_for_your_map_1
local map2button = the_button_for_your_map_2
local map3button = the_button_for_your_map_3
map1button.MouseButton1Click:Connect(function()
maps[1] += 1
end)
map2button.MouseButton1Click:Connect(function()
maps[2] += 1
end)
map2button.MouseButton1Click:Connect(function()
maps[3] += 1
end)
-- if you have more maps then you can add more buttons.
function startRound()
round = round + 1
local map = ""
if maps[1] >= maps[2] and maps[1] >= maps[3] then
map = "the number 1 map name"
elseif maps[2] >= maps[1] and maps[2] >= maps[3] then
map = "the number 2 map name"
elseif maps[3] >= maps[1] and maps[3] >= maps[2] then
map = "the number 3 map name"
-- if you have more maps do like this but with the number of your map.
end
print("Round "..round.." Start!")
--\\ !!this is will print the map but here you need to clone your map from a folder of all your maps!! //--
print("the map is: "..map)
-- insert your game logic here
for i, player in pairs(Players:GetPlayers()) do
-- process each player's round here
end
-- Countdown timer for game_time
spawn(function()
for i = set.game_time, 0, -1 do
print("Game ends in: "..i.." seconds.")
wait(1)
end
end)
wait(set.game_time) -- game runs for 'game_time' seconds
print("Round "..round.." End!")
-- making the all the maps 0 votes again
for i, v in pairs(maps) do
v = 0
end
end
function betweenRounds()
print("Waiting for the next round...")
-- Countdown timer for start_time
spawn(function()
for i = set.start_time, 0, -1 do
print("Next round starts in: "..i.." seconds.")
wait(1)
end
end)
wait(set.start_time) -- wait 'start_time' seconds between rounds
end
while true do
betweenRounds() -- start the countdown first
startRound() -- then start the round
end
ive created similar scripts for a game im working on. its a combat game with a round/loop system that has map voting and a timer. this is just helping you get ideas as idk what you have alkready done and you should be able to implement this code into yours.
So first in Replicated storage insert a boolvalue and string value.
Name the boolval “inRound” and stringval “Status”. Also insert 2 boolvalues called ‘map1’ and ‘map2’
In ServerScriptService insert and script and type this:
--define variables
local intermission = --how long you want intermission to be
local round = 180 --how long round to be
local inround = game.ReplicatedStorage.inRound
local status = game.ReplicatedStorage.Status
local random = math.random(1,2) --change '2' to a larger number if you have more maps
local map1 = --where ever it is, i assumed replicated storage
local map2 = --where ever it is, i assumed replicated storage
local map1val = game.ReplicatedStorage.map1
local map2val = game.ReplicatedStorage.map2
inround.Changed:Connect(function()
wait()
if inround.Value == true then
if random == 1 then
map1.Parent = game.Workspace
map1val.Value = true
else
map2.Parent = game.Workspace
map2val.Vlaue = true
--do whatever you want here like giving players a weapon or smth like that
print("inround")
elseif inround.Value == false then
print("intermission")
if map1val.Value == true then
map1.Parent = game.ReplicatedStorage
map1val.Value = false
else
map2val.Value == true then
map2.Parent = ReplicatedStorage
map2val.Value = false
--make sure to reset anything you did while inround == true
end
end)
--this is the countdown script
local function roundTimer()
while wait() do
for i = intermission, 1, -1 do
inround.Value = false
wait(1)
status.Value = "Intermission: "..i.." seconds left!"
end
for i = round, 1, -1 do
inround.Value = true
wait(1)
status.Value = "Game: "..i.." seconds left!"
end
end
end
spawn(roundTimer)
So thats a basic round system that is very flexible to do what you want in there. I found on how to do it in TheDevKing’s channel so go check him out if you want a better explanation.
If you want a GUI to display the countdown, insert this in StarterGui:
In the LocalScript, type this:
--define variables
local Status = game.ReplicatedStorage.Status
local TimerDisplay = script.Parent.TimerDisplay
Status.Changed:Connect(function() --happens when the stringvalue is changed
TimerDisplay.Text = Status.Value --updates the text to the number in the stringvalue
end)
so theres a basic round system with a gui that displays how much time is left. reply with more info on your scripts for help with anytthing else.
I’m going to give you some quick answers to all of your questions.
Game loop:
Simply just create a while loop in a server script and add a timer to it. When the timer runs out, the loop will repeat all over again.
Game modes and team modes:
You can use a variable to keep track of the current game mode and another variable for the current team mode. These variables can be changed during the game based on players’ choices or time elapsed, depending on your game mechanics.
Map randomization:
Create a list of available maps that players can vote for. When voting starts, send this list to the players so they can choose their favorite map.
Quick example of a map randomization script:
local avaibleMaps = {"DesertMap", "ArticMap", "SciFiMap", "TropicalMap", "WesternMap"}
local randomizedMaps = {}
for i = 1, 3 do
local randomMap = nil
repeat
local rng = Random.new()
randomMap = avaibleMaps[rng:NextInteger(1, #avaibleMaps)]
until not table.find(randomizedMaps, randomMap) --To make sure maps don't repeat.
table.insert(randomizedMaps, randomMap)
end
--You would then fire the randomizedMaps table to the client with a remote event.
Optimizing the scripts:
I don’t know much about script optimization but I guess you could use Module Scripts to create reusable and organized code that can be shared among various scripts.
Returning Player’s vote with a GUI.
Have a GUI with a Local Script that will request the randomizedMaps table from the server by using a Remote Event.
When a player clicks on a map, use Remote Events to notify the server about the player’s vote.
Afterwards get the map with the most votes and clone it into the Workspace.
I’ll also show you a simple script to check the map with the most votes:
local mapVotes = {
["DesertMap"] = 4,
["SciFiMap"] = 2,
["WesternMap"] = 5
}
local currentMostCount = 0
local currentMostName = nil
for name, count in pairs(mapVotes ) do
if (count >= currentMostCount) then
currentMostCount = count
currentMostName = name
end
end
print(("The map with the most votes is "..currentMostName.." with "..tostring(currentMostCount)" votes!")
--This would print "The map with the most votes is WesternMap with 5 votes!"