NPCs don't know who is an enemy and who is an ally?

Basically, I’m making a NPC that will fight NPCs of diffrent team colors, I have it detect every NPC in the workspace, And using a data folder detect who is what team.

It says the npc himself is an ally (Which is good, It detects that its on its own team)
But when i add another NPC of the same exact color, It appears to think it’s an enemy.

GIF of me placing 2 NPCs of the same team, But them thinking the other are enemies:
https://gyazo.com/b956f326b44a767fce7edc5620c62a16
(The 2 lines that say “Swordsman Friendly” are the NPCs saying they themselves are friendly to theirselves, The lines that say “Swordsman Enemy” Are the NPCs saying the other NPC is an enemy to them.)

Picture of the instances inside the NPC:
https://gyazo.com/41dd9cf636116be7c5322b3cff9374c5

Code used to determine if other NPCs are enemies or allies:
(TeamColor is a BrickcolorValue)

for _,Npc in pairs(workspace:GetDescendants()) do
	if Npc:FindFirstChild("Humanoid") and Npc:FindFirstChild("Data") then
		if Npc.Data.TeamColor.Value ~= script.Parent.Data.TeamColor.Value then
			table.insert(EnemyTable, Npc)
			print(Npc.Name, "Enemy")
		else
			table.insert(AllyTable, Npc)
			print(Npc.Name, "Friendly")
		end
	end
end

workspace.ChildAdded:Connect(function(Npc)
	if Npc:FindFirstChild("Humanoid") and Npc:FindFirstChild("Data") then
		if Npc.Data.TeamColor.Value ~= script.Parent.Data.TeamColor.Value then
			table.insert(EnemyTable, Npc)
			print(Npc.Name, "Enemy")
		else
			table.insert(AllyTable, Npc)
			print(Npc.Name, "Friendly")
		end
	end
end)
1 Like

what kind of value is TeamColor?

also try to change else to:

elseif Npc.Data.TeamColor.Value == script.Parent.Data.TeamColor.Value then
1 Like

Didn’t work, I assumed it didn’t change anything because the only other outcome for “else” was it being an ally.

It is a BrickcolorValue, It is assigned the player’s teamcolor when the NPC is spawned.

1 Like

i think it will work best with numbers, try that, if that does not work then…

Is no good day for programing

1 Like

How exactly would i use numbers? Since i’m trying to make the NPCs work with any kind of team, I can’t just assign team red a number of “1”

1 Like

i think there was way to convert BrickcolorValue into numer but im not 100% sure…

If its no longer posible then try to convert Brickcolor into Color3 and then just work with that that instead of Brickcolor

1 Like

I remember having to come across Team Filtering aha

Could you try printing what every NPC’s TeamColor value is supposed to be? Maybe that’ll clear up a bit more info

for _,Npc in pairs(workspace:GetDescendants()) do
	if Npc:FindFirstChild("Humanoid") and Npc:FindFirstChild("Data") then
		if Npc.Data.TeamColor.Value ~= script.Parent.Data.TeamColor.Value then
			table.insert(EnemyTable, Npc)
			print(Npc.Name, "Enemy", script.Parent.Data.TeamColor.Value)
		else
			table.insert(AllyTable, Npc)
			print(Npc.Name, "Friendly", script.Parent.Data.TeamColor.Value)
		end
	end
end

workspace.ChildAdded:Connect(function(Npc)
	if Npc:FindFirstChild("Humanoid") and Npc:FindFirstChild("Data") then
		if Npc.Data.TeamColor.Value ~= script.Parent.Data.TeamColor.Value then
			table.insert(EnemyTable, Npc)
			print(Npc.Name, "Enemy", Npc.Data.TeamColor.Value)
		else
			table.insert(AllyTable, Npc)
			print(Npc.Name, "Friendly", Npc.Data.TeamColor.Value)
		end
	end
end)
1 Like

Well while it isn’t the right teamcolor they still are the same.

https://gyazo.com/3df139adcf3998fdb5d80f3c575b7064

1 Like

Hm, maybe it could be the order you put your script in? I don’t think it’d make any difference but try this:

for _,Npc in pairs(workspace:GetDescendants()) do
	if Npc:FindFirstChild("Humanoid") and Npc:FindFirstChild("Data") then

		if Npc.Data.TeamColor.Value == script.Parent.Data.TeamColor.Value then
			table.insert(AllyTable, Npc)
			print(Npc.Name, "Friendly", script.Parent.Data.TeamColor.Value)

		elseif Npc.Data.TeamColor.Value ~= script.Parent.Data.TeamColor.Value then
			table.insert(EnemyTable, Npc)
			print(Npc.Name, "Enemy", script.Parent.Data.TeamColor.Value)
        else 
            print("Um w h a t")
		end
	end
end

workspace.ChildAdded:Connect(function(Npc)
	if Npc:FindFirstChild("Humanoid") and Npc:FindFirstChild("Data") then
		if Npc.Data.TeamColor.Value == script.Parent.Data.TeamColor.Value then
			table.insert(AllyTable, Npc)
			print(Npc.Name, "Friendly", script.Parent.Data.TeamColor.Value)

		elseif Npc.Data.TeamColor.Value ~= script.Parent.Data.TeamColor.Value then
			table.insert(EnemyTable, Npc)
			print(Npc.Name, "Enemy", script.Parent.Data.TeamColor.Value)
        else 
            print("Um w h a t")
		end
	end
end)

This is a weird issue if I’m being honest

It is still the exact same, I’m starting to think it’s brickcolorvalue that’s bugged?
Since my NPC placement system should have changed the color but didnt.

Could you try changing the BrickColor value if it fixes anything maybe?

Suprisingly, Now that i set the default starting color to blue, It seems to not be bugged anymore…

(Now i’m wondering why it’s doing weird things with medium stone grey?)

EDIT: When i spawn in an NPC it shows the value is blue, Yet it thinks of it as an enemy, Nothings changed apart from it starting as medium stone grey

My only assumption is that since the TeamColor is set to Medium stone grey, it’s set as Neutral for some reason

No clue though, just an assumption & guess

Does that mean i should give a diffrent default color?

If you want yeah, after all I see 2 Teams which are “Blue” & “Red” from the GIFs you’ve shown

Oh gosh darn it

Nevermind, Just tried and there’s the same issue.

Yeah, There’s blue and red, But i plan to make it so players can make their own teams with own colors.

This is a very strange issue, I don’t think its me, I think it’s because of roblox (not sure tho)

Weird, I’ma see if I can replicate it :thinking:

Alright, I’m gonna hit the hay, I’ll check back tommorow.

Take care :slight_smile:

1 Like

Ok so I did a bit of debugging, and I put this script inside 2 dummies:

local AllyTable = {}
local EnemyTable = {}

for _,Npc in pairs(workspace:GetChildren()) do
	if Npc:FindFirstChild("Humanoid") and Npc:FindFirstChild("Data") then

		if Npc.Data.TeamColor.Value == script.Parent.Data.TeamColor.Value then
			table.insert(AllyTable, Npc)
			print("Allies:")
			print(AllyTable)

		elseif Npc.Data.TeamColor.Value ~= script.Parent.Data.TeamColor.Value then
			table.insert(EnemyTable, Npc)
			print("Enemies:")
			print(EnemyTable)
		else 
			print("Um w h a t")
		end
	end
end

workspace.ChildAdded:Connect(function(Npc)
	if Npc:FindFirstChild("Humanoid") and Npc:FindFirstChild("Data") then
		if Npc.Data.TeamColor.Value == script.Parent.Data.TeamColor.Value then
			table.insert(AllyTable, Npc)
			print("Allies:")
			print(AllyTable)
			
		elseif Npc.Data.TeamColor.Value ~= script.Parent.Data.TeamColor.Value then
			table.insert(EnemyTable, Npc)
			print("Enemies:")
			print(EnemyTable)
		else 
			print("Um w h a t")
		end
	end
end)

I went ahead & ran the simulation, and this is what I got:

  • I first put a regular friendly Dummy in the workspace, and this is what outputted:
    image

  • Next I put a Evil Dummy in the workspace, and this is what outputted instead:
    image

    • First line is the Friendly Dummy’s Enemies

    • Second line is the Evil Dummy’s Enemies

    • Third line is the Evil Dummy’s Allies

If it sounds confusing, you could try the script I debugged & see what you get when you have a chance?