Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why are these values acting up?

BTW, this script is just part of the main script of a game where a red team goes against a blue team.

I've tried EVERYTHING with this script. Everything works properly, except the winning conditions. For some reason, the Red Team always wins. No matter how many players are on the team or how many players have died, the script just cuts off the round's for loop and makes the winning team red EVERY SINGLE TIME. Does anyone know what i did wrong here? Thank you so much for your time! The script is quite long, so sorry about that. The output just doesnt give me any errors or where it went wrong, so the whereabouts of this erorr in the script is just huge.

PS, BlueDeaths and RedDeaths values are set to 0 by default.

local RedDeaths = game.Workspace.GameScript.DeathMatchMode.Config.RedDeaths
local BlueDeaths = game.Workspace.GameScript.DeathMatchMode.Config.BlueDeaths
local WinningTeam

for i = 150,1,-1 do
	
	if BlueDeaths.Value == #BlueTeamPlayers then
		WinningTeam = game.Teams.Red
		break
	end
	if RedDeaths.Value == #RedTeamPlayers then
		WinningTeam = game.Teams.Blue
		break
	end
	
 status.Value = "Time Left: "..i 
    wait(1) 
end


status.Value = "Game End!"
FadeGameTrack()
game.Workspace.GameEffects.RoundEnd:Play()
wait(1)

--Red Team Win Conditions-------------------------------------------------

	if WinningTeam == game.Teams.Blue then
    	status.Value = "The Winner is the Blue Team!"
	end
	if WinningTeam == game.Teams.Red then
		status.Value = "The Winner is the Red Team!"
	end
	local Players = game:GetService("Players"):GetChildren() --Get all players
    for i=1, #Players do --Loop through all players
        if Players[i].Team == WinningTeam then --If the player is on the winning team then
            Players[i].leaderstats.Money.Value = Players[i].leaderstats.Money.Value + 10 --Reward
        end
    end
    wait(8)

1 comment

0

If there is nothing wrong with the script then there is a problem with the context. Like there is likely a problem with #RedTeamPlayers. Try simply using #game.Teams.Red:GetPlayers().

1 answer

✓ Accepted answer

To debug, you should add print statements to display values that don't seem to be working correctly. This will give you insight into what's going right/wrong. If you're testing with friends online, you can press F9 for the developer console. For instance, you might print after line 8:

print("Blue won", BlueDeaths.Value, #BlueTeamPlayers, RedDeaths.Value, #RedTeamPlayers)

and the same thing for Red, but with "Red won".

For example, say it printed out "Blue won" - then you'd know there's a problem with the second half of your script. More likely it will say "Red won" (unless the game times out), but at least you'll get to see all the numbers and get a clue as to what's going on. For example, maybe - despite both teams losing 1 player (with a team of size 2), it says Red won 2 2 0 2, which would indicate that, though it has the correct size of each team, the death counters are incrementing BlueDeaths regardless of what team the player is on.

Looking at what you've provided, I can see these potential problems:

  • You don't keep BlueTeamPlayers/RedTeamPlayers up-to-date. If a player leaves, they can no longer be killed - you should either update the team list or increase the death count.
  • Using BlueDeaths and RedDeaths: you realize that the same player could die 5 times (on a team of size 5) and then that team loses? Of course, this isn't a problem if - once dead - the player is taken off the team or respawns outside the fighting area.
  • If all remaining players die within the same second, blue will win, when maybe it should've been a draw. (This might be so rare that you don't mind leaving it as is.)

This discussion is preserved as it was. New answers, comments, and votes are closed.