How would I make it so that anyone that survives the round gets a win, not just the last person alive?

Hey guys, how would I change this script so as to make it that anyone that survives the round gets a win?

Right now its been made so that the last person alive gets a win, but how would I change this so that everyone that survives the round gets a win?

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Number = 0
local PositionX = 0
local PositionZ = 0

local Maps = ServerStorage:WaitForChild('Maps'):GetChildren()
local Status = ReplicatedStorage:WaitForChild('Status')

while true do
	
	--Intermission
	
	local Countdown = 30 -- intermission, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Intermission : '..Countdown
	until Countdown <= 0
	
	--Choose the map.
	
	Status.Value = 'Choosing Map...'
	
	local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
	local Spawns = ChosenMap:FindFirstChild('Spawns'):GetChildren()


	wait(3) -- little pause, make this as long as you want
	
	ChosenMap.Parent = workspace
	Status.Value = 'Map chosen, teleporting players.'
	
	wait(2) -- little pause, make this as long as you want
	
	--teleport the players
	
	local playersInRound = {} -- track alive players
	local connections = {} -- store connections to disconnect later
	for _, Player in pairs(Players:GetChildren()) do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			local RandomSpawn = Spawns[math.random(1, #Spawns)]
			Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
			
			

			table.insert(playersInRound, Player) -- add all players to table
			connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
				table.remove(playersInRound, table.find(playersInRound, Player))
			end)
		end
	end
	
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) -- remove player from list if they leave
		local ind = table.find(playersInRound, player)
		if ind then
			table.remove(playersInRound, ind)
		end
	end)
	
	Countdown = 5 -- Starting Round In, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Starting Round in : '..Countdown
	until Countdown <= 0
	
	Countdown = 120
	
	
	
	repeat wait(1.3)
		Countdown = Countdown - 1
		Number = math.random(1,36)
		PositionX = math.random(167,261)
		PositionZ = math.random(324,418)
		if Number == 1 or Number == 2 or Number == 3 or Number == 4 or Number == 5 or Number == 6 or Number == 7 or Number == 8 then
			local clone = game.ServerStorage.Bomb1:Clone()
			clone.Parent = game.Workspace.Bombs.Bombs
			clone.Main.Position = Vector3.new(PositionX,506.5,PositionZ)
		end
		if Number == 9 or Number == 10 or Number == 11 or Number == 12 or Number == 13 or Number == 14 or Number == 15 then
			local clone = game.ServerStorage.Bomb2:Clone()
			clone.Parent = game.Workspace.Bombs.Bombs
			clone.Main.Position = Vector3.new(PositionX,506.5,PositionZ)
		end
		if Number == 16 or Number == 17 or Number == 18 or Number == 19 or Number == 20 or Number == 21 then
			local clone = game.ServerStorage.Bomb3:Clone()
			clone.Parent = game.Workspace.Bombs.Bombs
			clone.Main.Position = Vector3.new(PositionX,506.5,PositionZ)
		end
		if Number == 22 or Number == 23 or Number == 24 or Number == 25 or Number == 26 then
			local clone = game.ServerStorage.Bomb4:Clone()
			clone.Parent = game.Workspace.Bombs.Bombs
			clone.Main.Position = Vector3.new(PositionX,506.5,PositionZ)
		end
		if Number == 27 or Number == 28 or Number == 29 or Number == 30 then
			local clone = game.ServerStorage.Bomb5:Clone()
			clone.Parent = game.Workspace.Bombs.Bombs
			clone.Main.Position = Vector3.new(PositionX,506.5,PositionZ)
		end
		if Number == 31 or Number == 32 or Number == 33 then
			local clone = game.ServerStorage.Bomb6:Clone()
			clone.Parent = game.Workspace.Bombs.Bombs
			clone.Main.Position = Vector3.new(PositionX,506.5,PositionZ)
		end
		if Number == 34 or Number == 35 then
			local clone = game.ServerStorage.Bomb7:Clone()
			clone.Parent = game.Workspace.Bombs.Bombs
			clone.Main.Position = Vector3.new(PositionX,506.5,PositionZ)
		end
		if Number == 36 then
			local clone = game.ServerStorage.Bomb8:Clone()
			clone.Parent = game.Workspace.Bombs.Bombs
			clone.Main.Position = Vector3.new(PositionX,506.5,PositionZ)
		end
		Status.Value = 'Ingame : '..Countdown
	until Countdown <= 0 or #playersInRound == 1

    -- Determine winner and give reward
    if #playersInRound == 1 then
        local success, message = pcall(function()
            local player = playersInRound[1]
            print("The Winner is: " .. player.Name)
            Status.Value = player.Name .. " won the game!"
            player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
            player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 300
            wait(3)
        end)
        if not success then
            warn("An error occurred while rewarding winner: " .. message)
        end
    else
        print("There was no single winner.")
        Status.Value = "There was no single winner this round."
    end

	--Kill the players
	for _, connection in pairs(connections) do -- disconnect connections to prevent memory leaks
		connection:Disconnect()
	end
	
	for _, Player in pairs(playersInRound)do
		Player:LoadCharacter()
	end
	
	ChosenMap:Destroy()
	
	Status.Value = 'Round Ended, waiting for new game.'
	
	wait(4) -- little pause, make this as long as you want.
	
end

Also, this system is a little buggy- any way to optimise it? Thanks!

1 Like

You could have a table with all the players before the round starts (basically your winners list) and if someone dies during the game, you remove them off the table.

5 Likes

Just insert a StringValue inside the player in the starting of the round.
When someone dies remove it.

Now after 120 seconds/2 mins check if the player has the StringValue.
If then reward them

2 Likes

From what it looks like, your code is already setup to work that way. You already have your table of playersInRound. You are also already removing the players and have the if-else statement at the end of the round that tests for the players. I would try to edit it this way (following code). If there are any errors with it then ask, but I didn’t try optimizing anything as I don’t have time. Hope this helps/works and feel free to ask more questions.

if #playersInRound > 1 then --- Check if there is more than one winner
    local success, message = pcall(function() --- I just used your pcall example
        local statusText = "The Winners are: " -- Creating a base message for the Status
        for i, player in pairs(playersInRound) do -- Loop through all winners
            statusText = statusText .. player.Name -- Concat the Status text with the player's name
            if i ~= #playersInRound then
                -- Add a comma if it is not the last player(winner)
                -- You want (player, player, player) not (player, player, player, )
                statusText = statusText ..  ", " 
            end
            -- Your stats code
            player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
            player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 300
        end
        print(statusText)
        Status.Value = statusText
    end)
    if not success then
        warn("An error occurred while rewarding winner: " .. message)
    end
elseif #playersInRound == 1 then
    local success, message = pcall(function()
        local player = playersInRound[1]
        print("The Winner is: " .. player.Name)
        Status.Value = player.Name .. " won the game!"
        player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
        player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 300
        wait(3)
    end)
    if not success then
        warn("An error occurred while rewarding winner: " .. message)
    end
else -- No Winners
    print("There was NO winner.")
    Status.Value = "There was no winner this round."
end
3 Likes

You could use the service called “CollectionService”. to give everyone a tag like “Survived”(Just an example). And when the round ends. You can check who has the tag and reward them

1 Like

Does anyone know how I could insert the code above by @Xo_doz into my pre existing code without damaging anything?

The basis of the code I provided was copied exactly from your code. That code would be the “Determine winner and give reward” second right before you kill the players.

2 Likes

What I would do if I was you is this.

From the server I would use .PlayerAdded and create a boolValue in the player and set it to false.

When the round starts i’ll do a loop setting all players value to true, well also setting the round value to true telling our system which a round is on.

When system detects the round value is set to true a while loop will start of and detect when the player dies for example…

while round do
      --// Code detecting the players deaths
     wait()
end

in this code when they die, you can set there value to false, and at the end of the round do another loop and all players with the value still at true you can add a win to there leaderstats!

Hope this helps!

1 Like

I wouldn’t recommend using wait() though. Theres a whole thread about it here:

1 Like

Set your table under a _G (Global Variable) for instance:

_G.Winners = winners

Now, loop through the table:

for _, v in ipairs(_G.Winners) do

 for x, y in ipairs(game.Players:GetChildren()) do

    if string.lower(tostring(v)) == string.lower(tostring(y.Name)) then

       --Any code here.
    end
end

end

1 Like

He already has that table. The problem is that he doesn’t know how to make every player in the table at the end of the round timer win.

2 Likes

There is also a similar post to avoid while true do I believe. I’m unsure though.

1 Like

Oh. I understand
30letters
30letters

1 Like