Attributes not updating

Hello, so I am running into the an issue where the Attributes in “Cap_Areas” isn’t updating once the function ResetGame is called. Not sure as to why would someone mind helping me out. This is a Server Script thats in ServerScriptService.

Teams = game:GetService("Teams")
Players = game:GetService("Players")

AttackersTeam = Teams:WaitForChild("UNSC")
DefendersTeam = Teams:WaitForChild("Celenix Federation")

Cap_Areas = workspace:WaitForChild("Cap_Areas")

Game_UI = game:WaitForChild("StarterGui").Game_UI
Score_UI = Game_UI:WaitForChild("Score")
ServerEvents = game.ReplicatedStorage.Events

function GetPlayersNear(Pos, CaptureDist)
	local Pos, CaptureDist = Pos, CaptureDist
	local Count_Attackers = 0
	local Count_Defenders = 0
	for _, Player in pairs (Players:GetPlayers()) do
		if Player.Character and Player.Character.Parent then
			local RootPart = Player.Character:FindFirstChild("HumanoidRootPart")
			if RootPart then
				local Dist = (RootPart.Position-Pos).Magnitude
				if Dist <= CaptureDist then
					if Player.TeamColor == AttackersTeam.TeamColor then
						Count_Attackers = Count_Attackers + 1
					end
					if Player.TeamColor == DefendersTeam.TeamColor then
						Count_Defenders = Count_Defenders + 1
					end
				end
			end
		end
	end
	return Count_Attackers, Count_Defenders
end

function ResetGame()
	print("Game reset! All capture points neutralized.")
	task.wait(5)
	for _, Cap_Area in pairs(Cap_Areas:GetChildren()) do
		local Team = Cap_Areas:FindFirstChild("Team")
		local Time = Cap_Areas:FindFirstChild("Time")
		
		if Team and Time then
			Team.Value = BrickColor.new("Institutional white")
			Time.Value = 0
		end
	end
	Cap_Areas:SetAttribute("Score_Defenders", 0)
	Cap_Areas:SetAttribute("Score_Defenders", 0)
	task.wait(1)
end

function HookUpCapArea(Area)
	local Area = Area
	local Team = Area:WaitForChild("Team")
	local Time = Area:WaitForChild("Time")
	local CaptureCircle = Area:WaitForChild("Circle")
	local Color = Score_UI.Holder.Objectives:GetChildren()
	local ToColor = Score_UI.Holder.Objectives:GetChildren().BackgroundColor3 == Color3.new(166,166,166)
	local InLoop = Instance.new("BoolValue", Area)
	InLoop.Name = "InLoop"
	local FullCapped = false
	local NeedsReturnToTime = 0
	local function StartLoop()
		if InLoop.Value then return end
		delay(0, function()
			InLoop.Value = true
			while true do
				local Count_Attackers, Count_Defenders = GetPlayersNear(CaptureCircle.Position, CaptureCircle.Size.X/2)
				local Empty = Count_Attackers == 0 and Count_Defenders == 0
				--
				if Empty then
					if not FullCapped then

						if NeedsReturnToTime == 0 then
							if Time.Value > 0 then
								Time.Value = Time.Value - 1
							elseif Time.Value < 0 then
								Time.Value = Time.Value + 1
							else
								break
							end
						else
							if NeedsReturnToTime < 0 then
								Time.Value = Time.Value - 1
								if Time.Value == Time.MinValue then
									break
								end
							elseif NeedsReturnToTime > 0 then
								Time.Value = Time.Value + 1
								if Time.Value == Time.MaxValue then
									break
								end
							end
						end
					else
						break
					end
				else
					local TimeChange = Count_Defenders - Count_Attackers -- based on number of players in cap, add/negate time value
					Time.Value = Time.Value + TimeChange
					FullCapped = math.abs(Time.Value) == Time.MaxValue
					if Time.Value == Time.MaxValue then
						Team.Value = DefendersTeam.TeamColor
						NeedsReturnToTime = Time.MaxValue
					elseif Time.Value == Time.MinValue then
						Team.Value = AttackersTeam.TeamColor
						NeedsReturnToTime = Time.MinValue
					elseif Time.Value == 0 then
						Team.Value = BrickColor.new("Institutional white")
						NeedsReturnToTime = 0
					end
				end
				--print(math.min(tick()), "//", Count_Attackers, "/", Count_Defenders)
				wait(1)
			end
			InLoop.Value = false
		end)
	end
	local StartLoopRemoteEvent = Instance.new("RemoteEvent", Area)
	StartLoopRemoteEvent.OnServerEvent:connect(function(Player)
		StartLoop()
	end)
	StartLoopRemoteEvent.Name = "StartLoopRemoteEvent"
end

for _, Cap_Area in pairs (Cap_Areas:GetChildren()) do
	HookUpCapArea(Cap_Area)
end


local Score_Attackers = 0
local Score_Defenders = 0
local ScoreToWin = Cap_Areas:GetAttribute("ScoreToWin")
Cap_Areas:SetAttribute("Score_Defenders", Score_Defenders)
Cap_Areas:SetAttribute("Score_Defenders", Score_Attackers)

while true do
	for _, Cap_Area in pairs (Cap_Areas:GetChildren()) do
		local Team = Cap_Area:FindFirstChild("Team")
		if Team then
			if Team.Value == AttackersTeam.TeamColor then
				Score_Attackers = Score_Attackers + 1
			end
			if Team.Value == DefendersTeam.TeamColor then
				Score_Defenders = Score_Defenders + 1
			end
			if Score_Defenders >= ScoreToWin then
				print("Celenix Federation has won!")
				for i, Player in pairs(Players:GetPlayers()) do
					Player:LoadCharacter()
				end
				task.spawn(ResetGame)
			elseif Score_Attackers >= ScoreToWin then
				print("The UNSC has won!")
				for i, Player in pairs(Players:GetPlayers()) do
					Player:LoadCharacter()
				end
				task.spawn(ResetGame)
			end
		end
	end
	print(Score_Attackers, "/", Score_Defenders)
	Cap_Areas:SetAttribute("Score_Attackers", Score_Attackers)
	Cap_Areas:SetAttribute("Score_Defenders", Score_Defenders)
	task.wait(10)
end
1 Like

To me, it looks like your While true do loop will always reset the attribute back to the value.

What I mean, you are using task.spawn. ResetGame function is behind called; however, because you are doing that it allows the loop to continue, instead of yield (wait). What’s most likely happening is, it is changing the value; however, IMMEDITELY afterwards the loop is setting the attributes to the below.

	print(Score_Attackers, "/", Score_Defenders)
	Cap_Areas:SetAttribute("Score_Attackers", Score_Attackers)
	Cap_Areas:SetAttribute("Score_Defenders", Score_Defenders)
	task.wait(10)

A possible solution would be to reset the value of the below lines in ResetGame or inside of the loop near where it calls the function.

local Score_Attackers = 0
local Score_Defenders = 0

You never reset the value of the local values, and will always reset the values. Aka never will return to zero (0)

1 Like

Thank you for the help I very much appreciate it.

1 Like

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