Script isn't working as it should no ERRORS

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

If you add some print statements, do they output?

print("I'm running!") --add one or more at the beginning/middle/end to verify the script is running.

The script is a “Script”, not a “LocalScript”, correct?

User print statements for debugging

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
1 Like

its a script. it print(“I’m running!”) at the very beginning of the script not in the middle part of the script

can you show us the explorer? what i want to take a look at is the team names specifically

Each team has a different colour assigned

Screen Shot 2024-06-09 at 9.02.38 AM

Nothing prints. I also had the player reset and still doesn’t print, but does put them on team neutral

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.

I put print(“1”) print(“2”) on every single line of code and nothing is printing.

Is there anymore code like is this just part of a script?

Yes there is, but I narrowed it down to this part of my script isn’t working its the bottom of my script.

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.

1 Like

Ok, I will try this.

emphasized text

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.