Value not increasing with repeat loop

i have a capture the flag script that increases the score when you are inside the area the problem is that it isnt going up

it prints “test2” but the score doesnt change

my script:

local RS = game:GetService("ReplicatedStorage")

local GS = RS.GermanyScore
local AS = RS.AmericaScore
local GermanyScore = 0
local AmericaScore = 0



local range = 50

local flag = script.Parent
local Teams = game:GetService("Teams")
local players = game:GetService("Players")

local Distance

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		task.wait()
		local HRP = char:WaitForChild("HumanoidRootPart")
		local Humanoid = char:WaitForChild("Humanoid")
		Distance = (HRP.CFrame.p - flag.CFrame.p).Magnitude
		
		while true do
			if Distance < range and Humanoid.Health > 0 and plr.TeamColor == BrickColor.new("White") then
				repeat
					GermanyScore = GermanyScore + 1
					print("test")
					wait(1)
				until
				Distance > range or Humanoid.Health == 0
			elseif Distance < range and Humanoid.Health > 0 and plr.TeamColor == BrickColor.new("Bright red") then
				repeat
					AmericaScore = AmericaScore + 1
					print("test2")
					wait(1)
				until
				Distance > range or Humanoid.Health == 0
			end
			print("Germany".."["..GermanyScore.."]".."-".."America".."["..AmericaScore.."]")
			AS.Value = AmericaScore
			GS.Value = GermanyScore
			task.wait(0.1)
			
		end
	end)
end)
1 Like

I could be wrong but I thought “.p” was deprecated? CFrame.Position will still return the position as a Vector3 without the rotation values.

The problem now is that it still adds up the score even when im out of range

Distance is only calculated at the start of plr.CharacterAdded:Connect(function(char) ... end) you need to update it inside of the repeat … until

1 Like