How to check if 2 or more players on different teams are standing a single part?

So I want to make it so when 2 or more people that are on different teams from each other, are standing on the “objective” part, that part gets a different color, but I don’t know how to do that. What would be the best solution to do that? I already make a script that sets the “objective” part’s color to the person’s team that is standing on the “objective” part.

Code:

local db = false
script.Parent.Touched:Connect(function(hit)
	if not db then
		db = true
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		
		if plr.Team == game.Teams.Red then
			script.Parent.ObjectiveBillboardGui.ObjectiveLabel.TextColor3 = Color3.fromRGB(255,0,0)
			script.Parent.SelectionBox.Color3 = Color3.fromRGB(255,0,0)
			script.Parent.Color = Color3.fromRGB(255,0,0)
		elseif plr.Team == game.Teams.Blue then
			script.Parent.ObjectiveBillboardGui.ObjectiveLabel.TextColor3 = Color3.fromRGB(0,0,255)
			script.Parent.SelectionBox.Color3 = Color3.fromRGB(0,0,255)
			script.Parent.Color = Color3.fromRGB(0,0,255)
		end
		
		
		if plr.Character.Humanoid.Health > 0 then
			plr.expLevel.Value += 100
		end
		
		wait(5)
		db = false
	end	
end)

Any help would be appreciated!

2 Likes

So tell me when two players on different teams are standing on this part you want to mix there teams colors and color the part with it or the color of the part back to white or the players die or what exactly

1 Like

Basically that yeah, I want the color of the part to turn white like in phantom forces in hard point.

You could use the :GetTouchingParts() function. This will return an array of instances touching the part. Note, if a certain leg is touching, it will only return the actual leg and not the character. To get the character, you can do game.Players:GetPlayerFromCharacter(instance.Parent)

1 Like

So I’d do something like, detecting the players using :GetTouchingParts(), then checking if those players are on the same team, if they are, set the color of the part to the color of the team that those players are on, if they are not the same team, set the part’s color to white. Would something like that work?

Yeah, when the part is touched check the others touching parts and check if their parents has humanoid so that mean that part is part from a player if you find two part from two player in different teams then the part will back to white

1 Like

Sure! This is how I imagine it.

local touchingParts = Part:GetTouchingParts()
local player1 = nil
local player2 = nil

for i,v in ipairs(touchingParts) do
  if v.Parent:FindFirstChild("Humanoid") then
    local player = game:GetService("Players"):GetPlayerFromCharacter(v.Parent)
    if player1 ~= nil then
      player2 = player
    else
       player1 = player
    end
    continue
  else
    continue
  end
end

if player1.TeamColor == player2.TeamColor then
   Part.BrickColor = player1.TeamColor
else
   Part.Color = Color3.fromRGB(255,255,255)
end
2 Likes

image
Got an error, not sure why, might be because team color uses brick colors to pick color not a color3 value, and that might cause an error, that is my best guess. Or a it’s a typo that is causing the error.

Well you shouldn’t copy my exact script because it has visible errors, I recommend using my script as a reference when creating yours.

1 Like

Yeah I was just testing if it worked, I am going to modify it to fit my exact situation, but I stumbled upon the error, and I have no idea why it’s happening, I’ll work on a solution myself. Thank you.

caleb just miss to check if the players exist:
try it like this:

local touchingParts = Part:GetTouchingParts()
local player1 = nil
local player2 = nil

for i,v in ipairs(touchingParts) do
  if v.Parent:FindFirstChild("Humanoid") then
    local player = game:GetService("Players"):GetPlayerFromCharacter(v.Parent)
    if player1 ~= nil then
      player2 = player
    else
       player1 = player
    end
    continue
  else
    continue
  end
end

if player1 ~= nil and player2 ~= nil then
    if player1.TeamColor == player2.TeamColor then
       Part.BrickColor = player1.TeamColor
    else
       Part.Color = Color3.fromRGB(255,255,255)
    end
end
player1 = nil
player2 = nil

1 Like

Hey sorry for the very late reply, I was busy for a while, then I forgot about the dev forum for a bit, sorry. Sadly, It still doesn’t work, no errors, nothing.

I did do some print functions for find out what wasn’t happening.

Image:

image

Here is the code with the print functions:

local Part = script.Parent
local touchingParts = Part:GetTouchingParts()
local player1 = nil
local player2 = nil

print("Variables have been set!")

for i,v in ipairs(touchingParts) do
	print("For loop has begun!")
	if v.Parent:FindFirstChild("Humanoid") then
		print("If statement has started!")
		local player = game:GetService("Players"):GetPlayerFromCharacter(v.Parent)
		print("Player variables has been set!")
		if player1 ~= nil then
			print("If statement has started x2!")
			player2 = player
		else
			print("Else thingy has been started!")
			player1 = player
		end
		print("Continue thingy.")
		continue
	else
		print("Else continue thingy.")
		continue
	end
end

if player1 ~= nil and player2 ~= nil then
	print("If statement has started x3!")
	if player1.TeamColor == player2.TeamColor then
		print("If statement has started x4!")
		Part.BrickColor = player1.TeamColor
	else
		print("Else thingy has been started x2!")
		Part.Color = Color3.fromRGB(255,255,255)
	end
end
player1 = nil
player2 = nil
print("Extra variables!")

As you can see, only “Extra variables” and “Variables have been set!” printed, even after I touched the part.

Video:

The old system works decently well, but in multiplayer it starts to break down. It takes 5 seconds for the capturing cooldown to go down, just so It wouldn’t lag in studio, but even with that, it’s still a mess. I also tried the code but replace FindFirstChild("Humanoid") to WaitForChild("Humanoid"), it still didn’t work.

As I said earlier, sorry for not responding for a while.

You need to add the touch event dont forgot it, try it like that:

local part = script.Parent
part.Touched:Connect(function()
	local touchingParts = Part:GetTouchingParts()
	local player1 = nil
	local player2 = nil
	for i,v in ipairs(touchingParts) do
	  if v.Parent:FindFirstChild("Humanoid") then
	    local player = game:GetService("Players"):GetPlayerFromCharacter(v.Parent)
	    if player1 ~= nil then
	      player2 = player
	      break
	    else
		  player1 = player
  	    end
	  end
	end
	if player1 ~= nil and player2 ~= nil then
 	   if player1.TeamColor == player2.TeamColor then
   	    Part.BrickColor = player1.TeamColor
  	   else
   	    Part.Color = Color3.fromRGB(255,255,255)
 	   end
	elseif player1 ~= nil  then
		Part.BrickColor = player1.TeamColor
	elseif player2 ~= nil then
		Part.BrickColor = player2.TeamColor
	end
end)

1 Like

Thank you, I didn’t know how :GetTouchingParts() worked, I now know. It does “work” but it is very glitchy, If a player leaves when 2 players are on it, it stays white and just glitches out. I tried to make an invisible can collide false detection part that changes the part’s color to the team color, but that still didn’t fix it. I think the problem would be fixed if there was to detect if a player has left the area, and have a really tall detection part so if you jump nothing would break, but I don’t know where I’d put the TouchEnded function to detect if a player left, so could you help me on that?

Also sorry once again for the late response, I don’t check notifications that often any more, you don’t need to make another script, just tell me what where to put it, and what should be inside of it. Thank you.

Use the same script with change Touched with TouchEnded

part.TouchEnded:Connect(function()
    wait(0.1)
	local touchingParts = Part:GetTouchingParts()
	local player1 = nil
	local player2 = nil
	for i,v in ipairs(touchingParts) do
	  if v.Parent:FindFirstChild("Humanoid") then
	    local player = game:GetService("Players"):GetPlayerFromCharacter(v.Parent)
	    if player1 ~= nil then
	      player2 = player
	      break
	    else
		  player1 = player
  	    end
	  end
	end
	if player1 ~= nil and player2 ~= nil then
 	   if player1.TeamColor == player2.TeamColor then
   	    Part.BrickColor = player1.TeamColor
  	   else
   	    Part.Color = Color3.fromRGB(255,255,255)
 	   end
	elseif player1 ~= nil  then
		Part.BrickColor = player1.TeamColor
	elseif player2 ~= nil then
		Part.BrickColor = player2.TeamColor
	end
end)

That will make the script check the players two times if part touched and also when the touch end and you need to add a little delay to add some smooth to your script because sometimes when the player walk on the part the touch will end

2 Likes

Where should I add the delay? The touch ended was happening randomly throughout testing.

I add it already just copy it like that and past it to you script and sorry for the late reponse i was so busy

1 Like