✓ Accepted answer
I've made dozens of round based games before. What I usually do is make a bunch of functions each with it's own purpose (e.g the intermission or choosing specific maps).
function Intermission()
for i = 30,0,-1 do
print(i)
wait(1)
end
end
function ChooseMap()
local Maps = game:GetService("ReplicatedStorage"):GetChildren()
local ChosenMap = Maps[math.random(1,#Maps)
print(ChosenMap.Name.." is the chosen map!")
local Map = ChosenMap:Clone()
Map.Name = "Map"
Map.Parent = workspace
end
function RunGame()
local Map = workspace.Map
for i,v in pairs(game:GetService("Players"):GetPlayers()) do
if v.Character and v.Character.Humanoid.Health > 0 then
Character.HumanoidRootPart.CFrame = Map.Spawns[i].CFrame + Vector3.new(0,3,0)
end
end
end
while true do
Intermission()
ChooseMap()
RunGame()
end
Considering you're attempting to check if the game is reaching its win/lose conditions, I usually make a loop for the game timer and have a loop that runs each second that checks if any of the players are alive and I use a conditional statement to check.
function RunGame()
local IngamePlayers = {}
local Map = workspace.Map
for i,v in pairs(game:GetService("Players"):GetPlayers()) do
if v.Character and v.Character.Humanoid.Health > 0 then
Character.HumanoidRootPart.CFrame = Map.Spawns[i].CFrame + Vector3.new(0,3,0)
table.insert(IngamePlayers,i)
end
end
for time = 100,0,-1 do
print(i)
for index,plr in pairs(IngamePlayers) do
if plr then
local char= plr.Character
if char.Humanoid.Health > 100 then
else
table.remove(IngamePlayers,index)
end
else
table.remove(IngamePlayers,index)
end
end
if #IngamePlayers == 1 then
print(IngamePlayers[1].Name.." has won!")
elseif #IngamePlayers == 0 then
print("everyone died! no one wins!")
elseif i == 0 then
print("ran out of time! no one wins!")
end
wait(1)
end
end
1 comment
The reason I ask is because I'm also pushing to write less procedural code and move more into OOP. Using a while loop over something like an event is just too procedural and "hacky". It doesnt seem like a good idea That being said I'm looking for more answers focused outside of that method of programming..