Capture the point system - Detect team so the team capture

Hello there,

ModelisiYT, our scripter, actually works on a point capture system that could detect the amount of players, and the current team of the player. He actually made the script so the character could capture the flag after a certain time. However, as he may be absent for next two weeks, I want to help him finishing this up. The fact is that I don’t know where to put the if condition in the code so if a “teamcolor” hits, it activates then do all operations I want (maybe Cframe later, change the flag, the text color of the point… I don’t know, I will see later).

Here is the code ;

local FlagCounterF = workspace.Point.Model.GUI.BillboardGui.Frame.Frame2
local Plate = script.Parent
local FlagValue = 60
local PlayerCount = 0
local ResetCounter = 5

Plate.Touched:Connect(function(Hit)
	local hitChildren = Hit.Parent:GetChildren()
	for i = 1, #hitChildren do
		print(hitChildren[i])
		if hitChildren[i].Name == "Humanoid" then
			PlayerCount = PlayerCount + 0.5
		end
	end
	ResetCounter = 5
	print(PlayerCount)
end)

Plate.TouchEnded:Connect(function(Hit)
	local hitChildren = Hit.Parent:GetChildren()
	for i = 1, #hitChildren do
		print(hitChildren[i])
		if hitChildren[i].Name == "Humanoid" then
			PlayerCount = PlayerCount - 0.5
		end
	end
	print(PlayerCount)
end)




while true == true do
	wait(1)
	print(PlayerCount)
	print(FlagValue)
	if FlagValue >= 0 then
		FlagValue = FlagValue - PlayerCount
		if FlagValue < 0 then
			FlagValue = 0
		end
	else
		FlagValue = 0
	end
	if PlayerCount == 0 then
		if ResetCounter >= 1 then
			ResetCounter = ResetCounter - 1
		else
			if FlagValue <= 55 then
				FlagValue = FlagValue + 5
			elseif FlagValue >= 56 then
				FlagValue = 60
			end
		end
	end
	print(FlagValue)
	
	FlagCounterF.Text = math.floor(FlagValue)
end

and the folder configuration of the flag.

I thought about putting the following ;

if player and player.TeamColor == BrickColor.new("Bright red") then

end

I’m not sure if this will work actually. I’m a novice into LUA, so if you could be also very clear in what you say, it could be better for me. Thanks for future answers, I would be happy if you could help me.

3 Likes

Could you elaborate? I’m unsure about what you mean.

1 Like

I also do not get what you are trying to do, or your intentions - scripting support is not for asking for code.

1 Like

You would want to use the Players service and use the function Players:GetPlayerFromCharacter() on Hit.Parent (the character). You would the confirm that the player exists within the server and if the player’s team is bright red through what you just wrote.

local Players = game:GetService("Players")
local player = Players:GetPlayerFromCharacter(Hit.Parent)
if player and player.TeamColor == BrickColor.new("Bright red") then
	--do what you need to here
end
2 Likes

I’m not even for asking code, since I already ask if I need to put the if statement before the while true == true do or inside to make one of the team capture the point. The fact is that I’ve been trying to place it before the while, or just before the FlagValue >= 0.

Thanks, 2Plants. I’ve been trying that if I remember, placing the local Players… If I place the Hit event, will it be in conflict with the first hit event that lies on the beggining of the code ? (Report to Plate.Touched:Connect(function(Hit) end)).

It’s not simple at all to explain that in English. I’m trying to do my best on my side

:wink:

1 Like

To begin, you shouldn’t have two events with basically the exact same code. This is repetitive and generally not good code practice. You can instead have a function containing the overlapping sections between the Touched and TouchEnded trigger and applying the function to the Hit parameter in both connections.

Furthermore, TouchEnded is a really iffy property that you should generally not rely on; it might not trigger when you want it to.

I’m unsure what you mean by “conflict.” You would most likely place the GetPlayerFromCharacter() function inside the Touched connection when the player actually comes into contact with the plate to execute what you describe instead of the while loop.

4 Likes