Help with capture point system

Hey devs!

Im currently working on a capture point system for my game, and i’ve run into a small problem
Im trying to make it so once the player goes onto the point, it detects the players team color and what ever color he is it adds a number onto the contested part, but when using.Touched you run into a problem. I can setup onto it once and it detects four, to 5 times.

CODE:

capturePoint.Touched:Connect(function(hitPart) --// detecting when the player hits the capture point
	local plr = game:GetService("Players"):GetPlayerFromCharacter(hitPart.Parent) --// Getting player from touched
	local hum = hitPart.Parent.Humanoid --// Getting humanoid from touched
	local db = false
	
	if plr and db == false then --// Detecting if player is still a  thing
		db = true
		if plr.TeamColor == teamA.TeamColor then --// Detecting if players team color matchs teamA
			print(plr.Name .. " has entered point on TeamA") --// Debuging'
			capturePointData.PointA.PlrTeamA.Value = capturePointData.PointA.PlrTeamA.Value + 1
			print("Point A currenlty has: " .. capturePointData.PointA.PlrTeamA.Value .. " of players on the point")
		end
	end
end)

EVERYTHING HERE WORKS I just need to find a way on how I can make it so it detects if the player gets onto the point, and only adds one value, and so once he gets off it removes him.

Example is that i just walked onto the point, it wont add four players only adds one.
So when I get off it get rids of the player.

Any help loved

So the players goes on, and it detects once and thats it.

1.
Be aware of that .Touched and .TouchEnded are not always “reliable”, so my following suggestions may and may-not work “always”.

2.
I see you already sort of know of ‘debounce’, as you have that db variable.

Unfortunately you seem to not yet know, that a local variable only “exist” for as long as the scope it is defined within exist. - So your placement of the db variable must be “outside” of the anonymous-function that is connected to the .Touched event.

3.
If you take a closer look at the sample codes, that the API documentation for Touched and TouchEnded has, you would see a “Touching Parts Count”. - That code may give you some clues, on how to only “count once”, combined with some debounce per player.

4.
To get a simple (and unfortunately not completely reliable and even slightly memory-leaking) “debounce per player” implementation, you could use an associative array, where the ‘key’ is the player-object and ‘value’ is how many parts-of-the-player’s-character that is “still touching”.

local playersTouching = {}

capturePoint.Touched:Connect(function(hitPart)
  -- (excluded code for finding player-object from hitPart)

  if player ~= nil then
    if playersTouching[player] == nil then
      -- Player-object was not already registered in the associative array,
      -- so we know that this must be the first time a part of the player's
      -- character is touching this capture-point. So initialize with zero,
      -- which will be incremented outside this if-statement's code-body.
      playersTouching[player] = 0

      -- (excluded code for score calculation)
    end

    -- Increase "still touching"
    playersTouching[player] = playersTouching[player] + 1
  end
end)

capturePoint.TouchEnded:Connect(function(hitPart)
  -- (excluded code for finding player-object)

  if player ~= nil then
    if playersTouching[player] ~= nil then
      -- Decrease "still touching"
      playersTouching[player] = playersTouching[player] - 1

      -- In case the count reaches or goes below zero, then remove
      -- the entire 'key' from this associative array.
      if playersTouching[player] <= 0 then
        playersTouching[player] = nil
      end
    end
  end
end)
2 Likes