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

Why is this script misinterpreting the break in a for loop?

Basically, in another script, each time a player dies on a team, the death value of the team is increased by 1. What this script is doing is breaking the rounds's for loop as soon as a team's death value equals to the number of players in that team. This will define the "WinningTeam" variable, either Red OR Blue. Everything works, except that the only way the for loop is broken is if a player leaves the game. I have no clue why, but maybe one of you guys can determine why the script is doing this. My script is below. Thanks for your time!;)

Round Script:

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

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


status.Value = "Game End!"
print(BlueDeaths.Value,RedDeaths.Value,WinningTeam)

Team Death Value Script:

Player = game.Players.LocalPlayer
local RedTeamDeaths = game.Workspace.Config.RedDeaths
local BlueTeamDeaths = game.Workspace.Config.BlueDeaths
humanoid = Player.Character:findFirstChild("Humanoid")

function DisableBackpack()
local StarterGui = game:GetService('StarterGui')
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
end

function DiedX_X()
if Player.Team == game.Teams.Red then
		print('1 red team player has died!')
		RedTeamDeaths.Value = RedTeamDeaths.Value + 1
end
if Player.Team == game.Teams.Blue then
		print('1 blue team player has died!')
		BlueTeamDeaths.Value = BlueTeamDeaths.Value + 1
end
DisableBackpack()
end

humanoid.Died:connect(DiedX_X)

1 answer

With FilteringEnabled, you cannot directly modify values on the server from the client.

I suggest you handle the deaths on the server.

This can be done using a PlayerAdded event, a CharacterAdded event, and a Died event.

--Declarations
local config = workspace.Config;
local RedDeaths,BlueDeaths,WinningTeam = config.RedDeaths,config.BlueDeaths;

--Death handler
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid");
		hum.Died:Connect(function()
			local val;
			if plr.Team == game.Teams.Red then
				val = RedDeaths;
			elseif plr.Team == game.Teams.Blue then
				val = BlueDeaths;
			end
			val.Value = val.Value + 1;
		end)
	end)
end)

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

status.Value = "Game End!"
print(BlueDeaths.Value,RedDeaths.Value,WinningTeam)

2 comments

0

Goulstem, that didnt seem to do really anything. Do you know of anything else i can try?

0

Welllll uhhh it should have. You could make sure that the values are actually being incremented.

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