Resetting Attributes through code

So what I’m trying to do is reset two attributes from their current value to 0 after the Max Score has been reached. Currently after the Max Score has been reached it doesnt reset it only prints which team has won.

This is the local Script that controls the UI

1.     local function TeamScores()
2. 	local Defenders_Score = ScoreUI.Defender.Score
3. 	local Attackers_Score = ScoreUI.Attacker.Score
4. 	Defenders_Score.Text = Cap_Areas:GetAttribute("Score_Defenders")
5. 	Attackers_Score.Text = Cap_Areas:GetAttribute("Score_Attackers")
6. 	local Attackers_ScoreBar = ScoreUI.Attacker.Capture.Bar.ScoreBar
7. 	local ACur = Cap_Areas:GetAttribute("Score_Attackers")
8. 	local Defender_ScoreBar = ScoreUI.Defender.Capture.Bar.ScoreBar
9. 	local DCur = Cap_Areas:GetAttribute("Score_Defenders")
10. 	local Max = Cap_Areas:GetAttribute("ScoreToWin")
11. 	Attackers_ScoreBar.Size = UDim2.new(ACur/Max,0,1,0)
12. 	Defender_ScoreBar.Size = UDim2.new(DCur/Max,0,1,0)
13. 	local function ResetMatch()
14. 		local MaxScore = Cap_Areas:GetAttribute("ScoreToWin")
15. 		local TeamColorChange = Cap_Areas:GetChildren()
16. 		if Defenders_Score or Attackers_Score >= MaxScore then
17. 			Defenders_Score = Cap_Areas:SetAttribute("Score_Defenders", 0)
18. 			Attackers_Score = Cap_Areas:SetAttribute("Score_Attacker", 0)
19. 			ScoreUI.Attacker.Score.Text = 0
20. 			TeamColorChange.Team.Value = "Institutional white"
21. 		end
22. 	end
23. end
24. TeamScores()
25. Cap_Areas:GetAttributeChangedSignal("Score_Defenders"):connect(TeamScores)
26. Cap_Areas:GetAttributeChangedSignal("Score_Attackers"):connect(TeamScores)

Here is the Server Script

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

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("The Republic Alliance has won!")
				for i, Player in pairs(Players:GetPlayers()) do
					Player:LoadCharacter()
				end
			elseif Score_Attackers == ScoreToWin then
				print("The Seperatist Alliance has won!")
				for i, Player in pairs(Players:GetPlayers()) do
					Player:LoadCharacter()
				end
			end
		end
	end
	--print(Score_Attackers, "/", Score_Defenders)
	Cap_Areas:SetAttribute("Score_Attackers", Score_Attackers)
	Cap_Areas:SetAttribute("Score_Defenders", Score_Defenders)
	wait(10)
end

These lines are wrong, they should be:

Cap_Areas:SetAttribute("Score_Defenders", 0)
Cap_Areas:SetAttribute("Score_Attacker", 0)
Defenders_Score = Cap_Areas:GetAttribute("Score_Defenders")
Attackers_Score = Cap_Areas:GetAttribute("Score_Attacker")

Looks like you’re making a Star Wars game…I’m interested :eyes:

I believe the issue is inside the local script controlling the UI.

It looks like you have the “ResetMatch” function inside it…however…that function is never called anywhere in the TeamScores function, therefore it’s just going to be idle when the Attackers_Score is above the MaxScore.

You should call that function below line 22 and it should work.

1 Like

You’re correct I am working on making Clone Wars era game. Based on the planet of Mygeeto.

1 Like