Kill Script Issue for my battle royale game

Hi! I am try to make a battle royale type game, I have made weapons that can kill players and NPCs.

I am trying to add a team mode, where I separate people to different teams, NPCs are not on a team though. when I did it though, I can only kill players, but not NPCs with this code, the “local team” is an object value.

--Variables for the sword and booleans
local sword = script.Parent
local canAttack = true
local canDamage = false

--onTouch function that runs when any part touches the Blade of the sword
function onTouch(part)
	
	local character = part.Parent
	local Ai = part.Parent:FindFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(character)
	if game.Workspace.TEAM.Value == true then
		if sword.Parent:IsA("Player") then
			local team = script.Parent.Team
			local parentTeam = sword.Parent
			local playerself = game.Players:GetPlayerFromCharacter(parentTeam)
			team.Value = playerself.Team
			if player.Team ~= team.Value then
				if player or Ai then
					if canDamage == true then
						if not character:FindFirstChild("Melee Shield") then
							character.Humanoid:TakeDamage(10)
						end
					end
				end
			end
		end	
	end
	--check if player exists and can damage is true, then deal the damage
	if game.Workspace.TEAM.Value == false then
		if player or Ai then
			if canDamage == true then
				if not character:FindFirstChild("Melee Shield") then
					character.Humanoid:TakeDamage(10)
				end
			end
		end
	end
	
end
sword.AxModel.Part.Touched:Connect(onTouch)

and this is the changed code to try to fix it. The result of this code is I can kill NPCs, but not players in team mode anymore. I changed the NPC’s humanoid name from Humanoid to AiHumanoid if that makes a difference.

--Variables for the sword and booleans
local sword = script.Parent
local canAttack = true
local canDamage = false

--onTouch function that runs when any part touches the Blade of the sword
function onTouch(part)
	
	local character = part.Parent
	local Ai = part.Parent:FindFirstChild("AiHumanoid")
	local player = game.Players:GetPlayerFromCharacter(character)
	if game.Workspace.TEAM.Value == true then
		if sword.Parent:IsA("Player") then
			local team = script.Parent.Team
			local parentTeam = sword.Parent
			local playerself = game.Players:GetPlayerFromCharacter(parentTeam)
			team.Value = playerself.Team
			if player then
				if player.Team ~= team.Value then
					if canDamage == true then
						if not character:FindFirstChild("Melee Shield") then
							character.Humanoid:TakeDamage(5)
						end
					end
				end
			end
		end
		if Ai then
			if canDamage == true then
				if not character:FindFirstChild("Melee Shield") then
					character.AiHumanoid:TakeDamage(5)
				end
			end
		end
	end
	--check if player exists and can damage is true, then deal the damage
	if game.Workspace.TEAM.Value == false then
		if player then
			if canDamage == true then
				if not character:FindFirstChild("Melee Shield") then
					character.Humanoid:TakeDamage(5)
				end
			end
		end
		if Ai then
			if canDamage == true then
				if not character:FindFirstChild("Melee Shield") then
					character.AiHumanoid:TakeDamage(5)
				end
			end
		end
	end
	
end
sword.Blade.Touched:Connect(onTouch)

The bottom line is, what happened between the two scripts, with the first one gave me the result of not being able to kill the npcs, to the second result, where I can kill the npcs but not the players?

I would change the AI’s humanoid name back to Humanoid and make it so the script does damage to whatever it hits as long as it’s parent has a humanoid in it rather than differentiating between the two.

or make the script find humanoids using “:IsA()” or “:FindFirstChild()”

Ok I will try that, I might have an issue though with the NPCs not being on a team, but I will see what I can do.

what is weird is that this works… this is a global function for my gun

--function for shooting a laser beam from a weapon tool
function GlobalFunctions.shootLaser(player, mouseHit, tool, maxDistance, damage, color, team)
	--create the ray itself from the tip to the position our mouse is clicking
	local ray = Ray.new(tool.Tip.CFrame.p, (mouseHit.p - tool.Tip.CFrame.p).unit * maxDistance)
	local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
	--create the look of the beam
	local beam = Instance.new("Part", workspace)
	beam.BrickColor = BrickColor.new(color)
	beam.FormFactor = "Custom"
	beam.Material = "Neon"
	beam.Transparency = 0.25
	beam.Anchored = true
	beam.Locked = true
	beam.CanCollide = false
	--cap the distance to the maxDistance value
	local distance = (tool.Tip.CFrame.p - position).magnitude
	if (distance > maxDistance) then
		distance = maxDistance
	end
	--change the size and direction of the beam
	beam.Size = Vector3.new(0.3, 0.3, distance)
	beam.CFrame = CFrame.new(tool.Tip.CFrame.p, position) * CFrame.new(0, 0, -distance/2)
	--destroy the beam after some timer
	game:GetService("Debris"):AddItem(beam, 0.1)
	--check if part is hit and if the part belongs to a humanoid
	if part then
		local humanoid = part.Parent:FindFirstChild("Humanoid")
		local Aihumanoid = part.Parent:FindFirstChild("AiHumanoid")
		--check another parent level if it has a humanoid
		if not humanoid then
			if not part.Parent.Parent:FindFirstChild("Gun Shield") then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
			end
			
		end
		if not Aihumanoid then
			Aihumanoid = part.Parent.Parent:FindFirstChild("AiHumanoid")
		end
		--do damage if humanoid
		if game.Workspace.TEAM.Value == false then
			if humanoid then
				local character = part.Parent
				if not character:FindFirstChild('Gun Shield') then
					humanoid:TakeDamage(damage)
				end
			end
			if Aihumanoid then
				local character = part.Parent
				if not character:FindFirstChild('Gun Shield') then
					Aihumanoid:TakeDamage(damage)
				end
			end
		end
		if game.Workspace.TEAM.Value == true then
			if humanoid then
				local character = part.Parent
				if not character:FindFirstChild('Gun Shield') then
					for i, player in ipairs(game.Players:GetPlayers()) do
						if player.Name == character.Name then
							if team ~= player.Team then
								humanoid:TakeDamage(damage)
							end
						end
					end	
				end
			end
			if Aihumanoid then
				local character = part.Parent
				if not character:FindFirstChild('Gun Shield') then
					Aihumanoid:TakeDamage(damage)
				end
			end
		end
	end
	
end

it will kill everything correctly