Game Loop System Help

In my game i have a simple game loop 1v1 but I have no idea how to code it so when a player dies or leaves the other player in the game wins how would i accomplish this?

local PlayersService = game:GetService(“Players”)

local intermissionMusic = workspace.Music.IntermissionMusic
local fightMusic = workspace.Music.FightMusic
local buildMusic = workspace.Music.BuildMusic

local tickSound = workspace.Sounds.Tick
local gameTick = workspace.Sounds.GameTick

local classicSword = game.ReplicatedStorage.ClassicSword

local lobby = workspace.Lobby
local maps = game.ReplicatedStorage.maps:GetChildren()
local status = game.ReplicatedStorage.Status

local gameTime = 60
local intermissionTime = 5
local minPlayer = 1

local function musictransition(off,on)
for i = 1, 10 do
off.Volume -= 0.005
task.wait(0.1)
end
on.Playing = true
on.TimePosition = 0
off.TimePosition = 0
off.Playing = false
for i = 1, 10 do
on.Volume += 0.005
task.wait(0.1)
end
end

while true do
for i = intermissionTime, 0, -1 do
status.Value = "intermission :smiley: : " … i
tickSound:Play()
task.wait(1)
end

local numPlayers = #PlayersService:GetPlayers()

if numPlayers < minPlayer then
status.Value = “need more players :(”
task.wait(3)

  for i = 10, 1, -1 do
  	status.Value = "waiting for more players: " .. i
  	tickSound:Play()
  	task.wait(1)
  end

  continue

end

local pickedMap = maps[math.random(1, #maps)]
local clonedMap = pickedMap:Clone()

musictransition(intermissionMusic,buildMusic)

for i = 5, 0, -1 do
status.Value = "generating map :slight_smile: : " … i
tickSound:Play()
task.wait(1)
end

status.Value = "map generated: " … clonedMap.Name
clonedMap.Parent = game.Workspace

musictransition(buildMusic,fightMusic)
for i = 5,1, -1 do
status.Value = i
tickSound:Play()
task.wait(0.7)
end
status.Value = “GO!”

task.wait(1)

local tps = pickedMap.Spawns:GetChildren()
local spawnedPlayers = {}

for _, player in pairs(PlayersService:GetPlayers()) do
local character = player.Character

  if character and #spawnedPlayers < 2 then
  	print(#spawnedPlayers)
  	local hrp = character.HumanoidRootPart
  	local randomSpawn
  	classicSword.Parent = player.Backpack

  	repeat
  		randomSpawn = tps[math.random(1, #tps)]
  	until not spawnedPlayers[randomSpawn]

  	spawnedPlayers[randomSpawn] = true
  	hrp.CFrame = randomSpawn.CFrame
  end

end

for i = gameTime, 0, -1 do
if i < 6 then
status.Value = "Fight >:D : " … i
gameTick:Play()
else
status.Value = "Fight >:D : " … i
end

  	task.wait(1)

end

for i, player in pairs(PlayersService:GetPlayers()) do
local character = player.Character

  if character then
  	classicSword.Parent = player.Backpack
  	local hrp = character.HumanoidRootPart
  	hrp.CFrame = lobby.Spawn.CFrame
  end

end

musictransition(fightMusic,intermissionMusic)

spawnedPlayers = {}
clonedMap:Destroy()
end

1 Like

I would probably do it by putting the 2 people in a table, and use a playerremoving/died event connected to each player and then once fired then remove the player from table and then you’ll know which player has won. You can use oop to make a table fire a function when a dictionary has been removed, but its not necessary.

how would i do this? like the putting the player in the table.

local DuelPlayers = {}

for _, player in pairs(PlayersService:GetPlayers()) do
local character = player.Character

  if character and #spawnedPlayers < 2 then
        DuelPlayers[player] = player
  	print(#spawnedPlayers)
  	local hrp = character.HumanoidRootPart
  	local randomSpawn
  	classicSword.Parent = player.Backpack

  	repeat
  		randomSpawn = tps[math.random(1, #tps)]
  	until not spawnedPlayers[randomSpawn]

  	spawnedPlayers[randomSpawn] = true
  	hrp.CFrame = randomSpawn.CFrame
  end
end

Something like this, you just add a table and do “DuelPlayers[player] = player” when putting players into match.

and so now i would do playerremoving and player died event

like this?

 PlayersService.PlayerRemoving:Connect(function(player)
 table.remove(playersinGame,player)
end)

Yeah, something like that but you would need to make sure to check that if the player is in the table otherwise remove it. But it’s not table.remove, it’s not an array, it’s a dictionary remember. It’ll be something like this “DuelPlayers[player] = nil”

1 Like

how would i check if hey are in the table or not? sorry ive never really learnt about tables until now

1 Like

It would be something like this, “if DuelPlayers[player] then”

so now how would i check if the duelplays value is less than 2? and if so then the one alive is the one who would be announced winner.

Well I’m not sure why you’re asking me this if you already done it before with this.

  if character and #spawnedPlayers < 2 then

so i did this

	for i = gameTime, 0, -1 do
		if #playersinGame <= 0 then
			status.Value = "gameover"
		else
			
			if i < 6 then
				status.Value = "Fight >:D : " .. i
				gameTick:Play()
			else
				status.Value = "Fight >:D : " .. i
			end

		task.wait(1)
		end
	end

but it automatically says game over when game starts

i printed #playersingame and it says 0

1 Like

heres my code for that

local playersinGame = {}

	for _, player in pairs(PlayersService:GetPlayers()) do
		local character = player.Character

		if character and #spawnedPlayers < 2 then
			playersinGame[player] = player
			print(playersinGame)

			local hrp = character.HumanoidRootPart
			local randomSpawn
			classicSword.Parent = player.Backpack

			repeat
				randomSpawn = tps[math.random(1, #tps)]
			until not spawnedPlayers[randomSpawn]

			spawnedPlayers[randomSpawn] = true
			hrp.CFrame = randomSpawn.CFrame
		end
	end
	print(#playersinGame)
1 Like

I’m not sure to be honest, make sure playersinGame is not inside some kind of a loop.

1 Like

yeah it is inside a while true loop

1 Like

i took the playersingame table outside of the while true loop and it still comesback as 0

come on man dont leave me like this

You have to yield the code before using :GetPlayers(), as it seems that you’re using it before any players actually join.

--[[
PlayersService.PlayerAdded:Wait()
OR
repeat task.wait() until #PlayersService:GetPlayers() >= 2
(If you want to wait for both players to exist)
]]
local playersinGame = PlayersService:GetPlayers()
print(#playersinGame)
	for _, player: Player in pairs(playersinGame) do
        --EDIT: Yielding the signal code until the character 100% exists
		local character = player.Character or player.CharacterAdded:Wait()

		if character and #spawnedPlayers < 2 then
			local hrp = character.HumanoidRootPart
			local randomSpawn
			classicSword.Parent = player.Backpack

			repeat
				randomSpawn = tps[math.random(1, #tps)]
			until not spawnedPlayers[randomSpawn]

			spawnedPlayers[randomSpawn] = true
			hrp.CFrame = randomSpawn.CFrame
		end
	end