Capturing system not working

Basically, the problem I am having is this loop keeps adding onto a value called ‘Capturers’, instead of just adding it once and leaving.

local Capturer
local FirstColor
	
local Capturers = 0
local Defenders = 0
	
local Range = 15
	
local ClosestPoint

local function giveCapturing(player, point)
	if player:FindFirstChild('Capturing') then
		local Capturing = player.Capturing
		Debris:AddItem(Capturing, 1)
	else
		local Capturing = Instance.new('ObjectValue')
		Capturing.Name = 'Capturing'
		Capturing.Value = point
		
		Capturing.Parent = player
	end
end

while true do
	-- Get nearby players to the Control Point
	for _, player in pairs(Players:GetPlayers()) do
		local Character = player.Character
		if Character then
			local HumanoidRootPart = Character.PrimaryPart
			if HumanoidRootPart then
				local Humanoid = Character:FindFirstChildWhichIsA('Humanoid')
				
                -- Look for the closest checkpoint to the player and make sure they are in range
				for _, v in pairs(ControlPoints:GetChildren()) do
					if player:DistanceFromCharacter(v.PrimaryPart.Position) <= Range then
						if Humanoid.Health > 0 then
							ClosestPoint = v
						end
					end
				end

                -- Player is near a control point
				if ClosestPoint then
                    -- Nobody has previously captured this point, so set this first player as the firstcolor
				 	if not FirstColor then
						FirstColor = player.TeamColor
					end
					if FirstColor then
						if player.TeamColor == FirstColor then
							if player.TeamColor ~= ClosestPoint.Team.Value then 
								print('Give', player, 'color') -- This prints infinitely
								Capturers = Capturers + 1
								giveCapturing(player, ClosestPoint)
							end
						else
							Defenders = Defenders + 1
							giveCapturing(player, ClosestPoint)
						end
					end
					Capturer = player
				else
					giveCapturing(player, ClosestPoint)
				end
			end
		end
	end
    wait()
end

The idea is that ‘Capturers’ should be the amount of players capturing the point, so if there are 4 players in the area, and they are on the same team and taking over the point, then capturers should be set to 4.

Control Points.rbxl (26.7 KB)

@NinjoOnline i might think you could add debounce to this script,but not very sure,i will need to read your script over again

I just added the place file, which has a bit of the code, but should make testing easier for anyone. Points to make, it’s inside a coroutine function, this needs to stay, as I have other stuff that goes into this script (not necessary atm)

you can make a variable for the amount of players if their in a current radius,and then make capturers value = player amount

Debounce is really needed!
image

Only reason I didn’t want a debounce is I’m not sure how to implement it in without causing a delay on anything? For example, if 1 player is capturing, then another player from the opposing team enters the area, it should immedialely hault any progress of the capture until either the capturer leaves or the defender leaves (capturing is done in the second ‘half’ of the script I guess you could call it :grimacing:)

I hope all the variable names, etc. make it somewhat easy to follow :grimacing:

you can easily prevent that
with a variable stating if player is capturing a point then

debounce == false -- you can always turn debounce off if you have a if statement or etc

A debounce wouldn’t stop the problem I’m having tho wouldn’t it?? If 1 player is capturing a point, then a second player joins (same team) in capturing said point, it should add +1 to the Capturers variable, as the more players on your team capturing increase the speed of the capture

ok im relate this to StrucidThey having a capturing gui showing how long it will take for you to capture(i think you will hvae the same thing)you can speed up the tweening process if more players are in radius)

Sorry if late,had to do something

Ye that’s the eventual idea, but looking at this section

if Capturers > 0 then
	if Defenders == 0 then
		local Ratio = 2.5

		if not Reset then
			if Percent < 100 then
				-- Capture amount
				Percent = Percent + (Capturers / Ratio)

The ‘Percent’ is hitting 100 immediately. The idea is that it should slowly increase based on the amount of capturers in the area.

umm idk since you dont know if Capturres is excalty 100

loop it

Figured it, problem I had was Capturers / Defenders and Capturer need to all be reset to 0/nil each loop thru.