How to fix my script?

Made a game script for a remaster of R.U.N a really fun game where you run from the little balls trying to eat you. However, once 2 players are in the game, it breaks somehow. I kept trying hard to fix it, nothing worked nor helped. If all died, it counts it as a victory still.

Please, help me fix it.

local alive = {}
local aliveplrs = 0

game.Players.PlayerAdded:Connect(function(plr)
print(plr.Name)
local char = plr.CharacterAdded:Wait()
char.Humanoid.Died:Connect(function()
for i = 1,#alive do
if alive[i] == plr.Name then
table.remove(alive,i)
aliveplrs = aliveplrs -1
print(plr.Name …“has been removed from the array!”)
else
print(“Player was not found.”)
end
end
end)
end)

function map()
workspace.Message.Text = “1 Minute til next game.”
workspace.lobby:Play()
wait(3)
workspace.lobby:Stop()
workspace.Five:Play()
wait(1)
workspace.Four:Play()
wait(1)
workspace.Three:Play()
wait(1)
workspace.Two:Play()
wait(1)
workspace.One:Play()
wait(1)

workspace.freshmeat:Play()
for i,v in pairs(game.Players:GetPlayers()) do
		v.Character.HumanoidRootPart.Position = Vector3.new(0, 40, -64)
		table.insert(alive,v.Name)
		aliveplrs = aliveplrs +i
		workspace.Message.Text = "A game is in progress."
		v.TeamColor = game.Teams.Alive.TeamColor
		local maps = game.Lighting.Maps:GetChildren()
local choice =  maps[math.random(1,#maps)]
choice:Clone().Parent = workspace
wait(40)
if aliveplrs >0 then
workspace.cheer:Play()
workspace.Message.Text = "Game Ended. Restarting..."
workspace[choice.Name]:Destroy()
wait(10)
map()
else
	workspace.evil:Play()
	workspace.Message.Text = "All Players died! Restarting Game..."
	workspace[choice.Name]:Destroy()
	wait(10)
	map()
end
	end
	end

map()

(sorry if not formatted correctly.)

Can you provide errors?

Thirty Characters

No errors were noticed nor seen.

Elaborate on how it breaks then?

So, once a second player joins, it breaks because if one dies, its a victory. I don’t know why.

Can you paste your code into a pastebin? It’s sort of annoying trying to deal with it this post being unformatted.

local alive = {}local aliveplrs = 0game.Players.PlayerAdded:Connect(functi - Pastebin.com, here you go.

local aliveplrs = 0 is the same as #alive
you can remove local aliveplrs

(edited)
local alive = {}
--local aliveplrs = 0

game.Players.PlayerAdded:Connect(function(plr)
print(plr.Name)
local char = plr.CharacterAdded:Wait()
char.Humanoid.Died:Connect(function()
for i, v in pairs(alive) do
if v == plr.Name then
table.remove(alive,v)
--aliveplrs = aliveplrs -1
print(plr.Name.."has been removed from the array!")
else
print("Player was not found.")
end
end
end)
end)

Where is this script in your game?

It’s in the workspace, mostly where alot of game scripts would be for classic games and more.

Put it in the ServerScriptService

I’m just gonna wait here patiently for a reply.

Have you tried moving the script into ServerScriptService? Also, are there any other scripts that run once a player enters the game? Please don’t be passive aggressive, and bumping your post like that is against forum rules.

To format it properly(if it’s not the first time), select all of the code then press “preformatted text” which is located 7th to the right of the buttons you can press.

I was just saying. I aint passive aggressive.

I guess using a quote made it confusing. I was calling OP passive agressive.

I think its a misunderstanding