Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 8 years ago
Edited 8 years ago

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:

01local RedDeaths = game.Workspace.Config.RedDeaths
02local BlueDeaths = game.Workspace.Config.BlueDeaths
03local WinningTeam
04 
05for i = 150,1,-1 do
06 
07    if BlueDeaths.Value == #game.Teams.Blue:GetPlayers() then
08        WinningTeam = game.Teams.Red
09        break
10    end
11    if RedDeaths.Value == #game.Teams.Red:GetPlayers() then
12        WinningTeam = game.Teams.Blue
13        break
14    end
15 
View all 22 lines...

Team Death Value Script:

01Player = game.Players.LocalPlayer
02local RedTeamDeaths = game.Workspace.Config.RedDeaths
03local BlueTeamDeaths = game.Workspace.Config.BlueDeaths
04humanoid = Player.Character:findFirstChild("Humanoid")
05 
06function DisableBackpack()
07local StarterGui = game:GetService('StarterGui')
08StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
09end
10 
11function DiedX_X()
12if Player.Team == game.Teams.Red then
13        print('1 red team player has died!')
14        RedTeamDeaths.Value = RedTeamDeaths.Value + 1
15end
View all 23 lines...

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago
Edited 8 years ago

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.

01--Declarations
02local config = workspace.Config;
03local RedDeaths,BlueDeaths,WinningTeam = config.RedDeaths,config.BlueDeaths;
04 
05--Death handler
06game.Players.PlayerAdded:Connect(function(plr)
07    plr.CharacterAdded:Connect(function(char)
08        local hum = char:WaitForChild("Humanoid");
09        hum.Died:Connect(function()
10            local val;
11            if plr.Team == game.Teams.Red then
12                val = RedDeaths;
13            elseif plr.Team == game.Teams.Blue then
14                val = BlueDeaths;
15            end
View all 36 lines...
0
Goulstem, that didnt seem to do really anything. Do you know of anything else i can try? Seabiscuitz 21 — 8y
0
Welllll uhhh it should have. You could make sure that the values are actually being incremented. Goulstem 8144 — 8y
Ad

Answer this question