Checking for team not working

So my capture the flag script checks if youre in a team and then give the points to that team
the problem is no matter what team i am in the points only goes to Germany

my script:

local GermanyScore = 0
local AmericaScore = 0
local range = 200

local flag = script.Parent

local Teams = game:GetService("Teams")

local players = game:GetService("Players")

local function FindPlayerNearFlag()
	local PlayerTable = players:GetPlayers()

	for _,v in pairs (PlayerTable) do
		local Character = v.Character
		if not Character then return end

		local HRP = Character:WaitForChild("HumanoidRootPart")
		local Humanoid = Character:WaitForChild("Humanoid")

		local Distance = (HRP.CFrame.p - flag.CFrame.p).Magnitude

		if Distance < range and Humanoid.Health > 0 and v.TeamColor == BrickColor.new("White") then
			GermanyScore = GermanyScore + 1
		elseif Distance < range and Humanoid.Health > 0 and v.TeamColor == BrickColor.new("Bright red") then
			AmericaScore = AmericaScore + 1
		end
	end
end


	while true do
		FindPlayerNearFlag()
		print(GermanyScore.." Germany")
		print(AmericaScore.." America")
		wait(1)
	end
1 Like

1- Use task.wait and not wait.
2-You’re While looping, means it’d call the function forever. And that’s why the error occurs.
3- You should add an if statement or replace the while loop with a repeat loop, and by that, you’ll need to make sure that function will fun only when the player is in the range.

You can try something like this :

local GermanyScore = 0
local AmericaScore = 0
local range = 200

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
				GermanyScore +=1
			elseif Distance < range and Humanoid.Health > 0 and plr.TeamColor == BrickColor.new("Bright red") then
				AmericaScore +=1
			end
			print("Germany".."["..GermanyScore.."]".."-".."Amercia".."["..AmericaScore.."]")
			task.wait(1)
		end
	end)
end)



I only get points for Germany and my character is in America

Change your team through the server, for me it worked fine

i did, but it still says that i get points for Germany

Okay, it works now for some reason

Could you send a video? it’d help

it works now but it only works when i spawn in the area of the flag
if i spawn out of the area and enter it i get no points and if i spawn in the area and get out of the area i still get points

Could you send a video? It’d help, because for me it works

You see that im moving out of range but the output says that the scores increases

Roblox1