Script in serverscriptservice doesn’t work no errors in output either
-- Function to check remaining players on each team
local function checkRemainingPlayers()
local teamCounts = {
TeamA = 0,
TeamB = 0,
TeamC = 0,
TeamD = 0
}
-- Count players in each team
for _, player in ipairs(Players:GetPlayers()) do
if player.Team and teamCounts[player.Team.Name] ~= nil then
teamCounts[player.Team.Name] = teamCounts[player.Team.Name] + 1
end
end
if teamCounts.TeamA == 0 then
for teamName, count in pairs(teamCounts) do
if teamName ~= "TeamA" and count == 1 then
for _, player in ipairs(Players:GetPlayers()) do
if player.Team and player.Team.Name == teamName then
if player.Name == V.Value then
print(player.Name .. " WINS!")
GameVal.Value = false
return
end
end
end
end
end
end
end
-- Function to handle player death
local function onPlayerDeath(player)
print("Player " .. player.Name .. " has died.")
if player.Team then
local neutralTeam = Teams:FindFirstChild("Neutral")
if neutralTeam then
player.Team = neutralTeam
-- Optionally, respawn the player
player:LoadCharacter()
else
warn("Neutral team not found!")
end
end
-- Check remaining players on each team
checkRemainingPlayers()
end
-- Function to setup the player
local function onPlayerAdded(player)
local function onCharacterAdded(character)
print("Character added for player " .. player.Name)
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Died:Connect(function()
onPlayerDeath(player)
end)
end
end
-- Connect the CharacterAdded event
player.CharacterAdded:Connect(onCharacterAdded)
-- Handle the case where the character is already added
if player.Character then
onCharacterAdded(player.Character)
end
end
-- Connect the PlayerAdded event
Players.PlayerAdded:Connect(onPlayerAdded)
-- Handle players that are already in the game (e.g., during script reloads)
for _, player in ipairs(Players:GetPlayers()) do
onPlayerAdded(player)
end
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local GameVal = game.Workspace:WaitForChild("GameVal")
local V = game.Workspace:WaitForChild("V")
local function checkRemainingPlayers()
local teamCounts = {
TeamA = 0,
TeamB = 0,
TeamC = 0,
TeamD = 0
}
for _, player in ipairs(Players:GetPlayers()) do
if player.Team and teamCounts[player.Team.Name] ~= nil then
teamCounts[player.Team.Name] = teamCounts[player.Team.Name] + 1
end
end
print("Team counts:", teamCounts.TeamA, teamCounts.TeamB, teamCounts.TeamC, teamCounts.TeamD)
if teamCounts.TeamA == 0 then
for teamName, count in pairs(teamCounts) do
if teamName ~= "TeamA" and count == 1 then
for _, player in ipairs(Players:GetPlayers()) do
if player.Team and player.Team.Name == teamName then
if player.Name == V.Value then
print(player.Name .. " WINS!")
GameVal.Value = false
return
end
end
end
end
end
end
end
local function onPlayerDeath(player)
print("Player " .. player.Name .. " has died.")
if player.Team then
local neutralTeam = Teams:FindFirstChild("Neutral")
if neutralTeam then
player.Team = neutralTeam
player:LoadCharacter()
else
warn("Neutral team not found!")
end
end
checkRemainingPlayers()
end
local function onPlayerAdded(player)
local function onCharacterAdded(character)
print("Character added for player " .. player.Name)
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Died:Connect(function()
onPlayerDeath(player)
end)
else
warn("No humanoid found for player " .. player.Name)
end
end
player.CharacterAdded:Connect(onCharacterAdded)
if player.Character then
onCharacterAdded(player.Character)
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
for _, player in ipairs(Players:GetPlayers()) do
onPlayerAdded(player)
end
What exactly doesn’t work currently? I mean like what part of the script doesn’t work, as I feel like a have a vague understanding of the problem at hand which means I can’t help you, even though I would love to help and fix the problem.
The only thing that seems to be working is once the player dies they are switched back to team neutral. Everything else seems to not be working. As once all the players are dead and are removed from the teams and put on Team neutral and there is only when remaining player on one of the teams then it should print he wins.
MMM, alright it would be easier to help you if you showed the whole script. I would recommend either using 2 separate scripts by using bindible events or use bindible events in the script you have now.