Hey so I am making a game with 10 rounds and at the 10th round I want the players left to get a win
I have already made the table : local players = game.Players:GetPlayers(); -- Table of players in server
I have the script that removes players from the table when they die :
local function playerAdded(player)
local conn -- disconnect connections or fraud
local function characterAdded(character)
if not character.Parent then
character.AncestryChanged:Wait()
end
local function onDied()
conn:Disconnect()
table.remove(players, table.find(players, player))
end
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid.Died:Connect(onDied)
end
end
local character = player.Character
if character then
characterAdded(character)
end
conn = player.CharacterAdded:Connect(characterAdded)
end
local Players = game:GetService("Players")
for _, player in ipairs(Players:GetPlayers()) do
coroutine.wrap(playerAdded)(player)
end
All I need is help on the script that I will place at the end of round 10 that will give players left in the table the WIN (leaderstats already made). Additionally if you could help me with a script that after giving the win, removes the rest of the players in the table and then my script will loop.
ROUND 10 SCRIPT IF NEEDED =
Circles = game.Workspace.Circles:GetChildren()
Chosen_Colour = Circles[math.random(1,#Circles)]
game.ReplicatedStorage.Colour.Value = Chosen_Colour.Name
task.wait(2)
-- ROUND 9,10
for number,Circle_Colours in pairs(Circles) do
for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
Circle_Part.Transparency = 1
Circle_Part.CanCollide = false
end
end
for number, Circle_Part in pairs(Chosen_Colour:GetChildren()) do
Circle_Part.Transparency = 0
Circle_Part.CanCollide = true
end
wait(3)
for number,Circle_Colours in pairs(Circles) do
for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
print("h")
Circle_Part.Transparency = 0
Circle_Part.CanCollide = true
end
end
wait(2)
print("Round 9 and 10 completed")
local StarterBlock = game.Workspace.StarterBlock
StarterBlock.Transparency = 0
StarterBlock.CanCollide = true
local Players = game:GetService("Players")
for _, Player in pairs(Players:GetChildren()) do
local leaderstats = Player.leaderstats
local Wins = leaderstats.Wins
Wins.Value += 1
end
game.ReplicatedStorage.Colour.Value = "Game ended."
wait(3.5)
game.ReplicatedStorage.Colour.Value = " "
local PlayersService = game:GetService("Players")
local Players = PlayersService:GetPlayers()
local PlrAddConn, PlrRemoveConn, CharAddConn, CharRemoveConn, DiedConn = nil, nil, nil, nil, nil
PlrConn = PlayersService.PlayerAdded:Connect(function(Player)
if PlrRemoveConn then
PlrRemoveConn:Disconnect()
end
CharAddConn = Player.CharacterAdded:Connect(function(Character)
if DiedConn then
DiedConn:Disconnect()
end
if CharRemoveConn then
CharRemoveConn:Disconnect()
end
local Humanoid = Character:WaitForChild("Humanoid")
DiedConn = Humanoid.Died:Connect(function()
if table.find(Players, Player) then
table.remove(Players, table.find(Players, Player))
end
end)
end)
local CharRemoveConn = Player.CharacterRemoving:Connect(function(Character)
if CharAddConn then
CharAddConn:Disconnect()
end
end)
end)
PlrRemoveConn = PlayersService.PlayerRemoving:Connect(function(Player)
if PlrConn then
PlrConn:Disconnect()
end
end)
for _, player in ipairs(Players) do --change "Players" to name of table alive players are stored inside
local leaderstats = player:WaitForChild("leaderstats")
if leaderstats then
local Wins = leaderstats:WaitForChild("Wins")
if Wins then
Wins.Value += 1
end
end
end
You likely wouldnât need to fetch the player from game.Players/PlayersService as âvâ in your implementation will already be a reference to the player object.
local tables = {"a", "b", "c"}
local tables = {} --overrides previous table with an empty table (emptying it)
table.clear(tables) --alternatively this function from the table library will achieve the same
You can use either of these methods to empty a table (be it an array or dictionary) which you choose is entirely up to you.
Thatâs because youâre attempting to pass the PlayerService itself as an argument to the ipairs iterator function as opposed to an array (which is what is required).
local Players = game:GetService("Players")
local PlayerList = Players:GetPlayers()
for _, player in ipairs(PlayerList) do --change "PlayerList" to name of table alive players are stored inside
local leaderstats = player:WaitForChild("leaderstats")
if leaderstats then
local Wins = leaderstats:WaitForChild("Wins")
if Wins then
Wins.Value += 1
end
end
end
Make sure you donât override any existing variables with the top two variables.
Alright and so for the âremoving of dead playersâ script, I donât want to mess the script up but where will I need to make the changes if the table of players is named âPlayersInServerâ.
Additionally where will I need to place this script?
local function onDied()
conn:Disconnect()
if table.find(PlayersInServer, player) then
table.remove(PlayersInServer, table.find(PlayersInServer , player))
end
end