I have a winner script in my game, if someone wins, it announces it at the end of the round.
Right now it only works with one person in the game for some reason.
So far, I tried looking on the Developer Hub for solutions, and found something to make this script possible, but it still only works with one person in the server.
`local round_players = {}
local maps = require(game.ServerScriptService.Maps)
local x = 0
while true do
wait(0)
for i, v in pairs(maps) do
if game.Workspace.MapStorage:FindFirstChild(v) then
local round_players = {}
for i, player in pairs(game.Players:GetPlayers()) do
table.insert(round_players, player)
repeat
wait(0)
x = x+1
print(x)
player.Character.Humanoid.Died:Connect(function()
print(player.Name … " has died!")
table.remove(round_players, i)
end)
until x == 1350
x = 0
for i, player in pairs(game.Players:GetPlayers()) do
local msg = ""
for i, player in pairs(round_players) do
if player and player.Parent then
msg = msg.."Winners: "..player.Name
end
end
msg = msg..""
player.PlayerGui.MapNameGui.TextLabel.Text = msg
end
end
end
end
end`
I’m very sorry if this script is confusing. Thank you for reading!
Edit: Sorry the script formatted weird. I don’t know why it did that.
local round_players = {}
local maps = require(game.ServerScriptService.Maps)
local x = 0
while true do
wait(0)
for i, v in pairs(maps) do
if game.Workspace.MapStorage:FindFirstChild(v) then
local round_players = {}
for i, player in pairs(game.Players:GetPlayers()) do
table.insert(round_players, player)
repeat
wait(0)
x = x+1
print(x)
player.Character.Humanoid.Died:Connect(function()
print(player.Name … " has died!")
table.remove(round_players, i)
end)
until x == 1350
x = 0
for i, player in pairs(game.Players:GetPlayers()) do
local msg = ""
for i, player in pairs(round_players) do
if player and player.Parent then
msg = msg.."Winners: "..player.Name
end
end
msg = msg..""
player.PlayerGui.MapNameGui.TextLabel.Text = msg
end
end
end
end
end
Here is the script with the code block formatted correctly!
That part checks if any player has died and if I make the wait longer than Wait() it won’t work very well. I put it as 1350 because it’s about 45 seconds with waits in between. Tthank you for responding!
Also wouldn’t your script do something like: Winners: playername Winners: playername2 Winners: playername3
since you just append Winners: + name each time? Is that intended?
You should check that, also is the fact that the loop will loop through all the players that are in round_players for each player resulting in a lot of looping, it would be better if you put the round_players loop and then just update everyone’s TextLabel at once afterwards.
Don’t use the repeat loop. (hence, no need for x variable)
Add a wait(number) after the (line 10) for loop ends.
You’re connecting .Died 1350 times. (because of the repeat loop)
You never disconnect .Died
Players who leave the game have not been removed from the round_players array.
The (line 24) for loop should be outside of the (line 10) for loop and can be replaced with:
wait(45) --or whatever u want
for i, player in pairs(game.Players:GetPlayers()) do --Move this out of the for loop on line 10
local winners = table.concat(round_players,", ")
player.PlayerGui.MapNameGui.TextLabel.Text = "Winners: "..winners --i suggest using remoteEvents
end
Oh, wait() also works fine, wait(0) is unnecessary.