Sword detecting monsnters

Hi everyone. I have a sword that does not do team damage to players on the same team. However that caused an issue for the sword not doing damage to monsters because they are not on a team.

I put a part inside the monster and welded it to the torso. The part is called Mob. Is there a way to have the script detect if the humanoid has that part then it will also do damage to the monster. Ill link the swords damage script below.

local weapon = script.Parent
local dmg = 50 -- Change the damage value here
local hitsound = script.Parent.Parent.Handle.hitsound


weapon.Touched:connect(function(part)
	if part.Parent:findFirstChild("Humanoid") then
		local humanoid = part.Parent:findFirstChild("Humanoid")
		local character = humanoid.Parent
		local hitPlayer = game.Players:GetPlayerFromCharacter(character)
		local wieldingPlayer = game.Players:FindFirstChild(weapon.Parent.Parent.Name)
		if hitPlayer.Team ~= wieldingPlayer.Team then
			humanoid:TakeDamage(dmg)
			hitsound:Play()
			script:Destroy()
		end
	end
end)

I was thinking changing

		if hitPlayer.Team ~= wieldingPlayer.Team then

to something like

		if hitPlayer.Team ~= wieldingPlayer.Team or (humanoid has part Mob)

I have tried to enter it a few different ways but have been unsuccessful. So I wrote in what I think needs to happen.

Does anyone agree this would solve the issue? Or is there a better way to achieve this goal.

Thanks very much in advance. I always appreciate your help.

1 Like
local weapon = script.Parent
local dmg = 50 -- Change the damage value here
local hitsound = script.Parent.Parent.Handle.hitsound
local players = game:GetService("Players")

weapon.Touched:connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") then
		local hitPlr = players:GetPlayerFromCharacter(part.Parent)
		local character = hitPlr.Character
		local humanoid = character:FindFirstChild("Humanoid")
		local hittingPlr = players:GetPlayerFromCharacter(weapon.Parent)
		if not hitPlr.TeamColor then --hitting an npc that isnt in a team
			--do code
			return
		end
		if hitPlr.TeamColor ~= hittingPlr.TeamColor then
			humanoid:TakeDamage(dmg)
			hitsound:Play()
		end
	end
end)

since you’re working with ‘‘teams’’
mobs must have their own ‘‘team’’ too, otherwise they won’t be counted as something in your function

For example
i would do this:

local character = humanoid.Parent
	local hitPlayer = game.Players:GetPlayerFromCharacter(character)
	local wieldingPlayer = game.Players:FindFirstChild(weapon.Parent.Parent.Name)
    local Mob = game.Workspace:FindFirstChild(Reference of a Model with humanoid here)  

	if hitPlayer.Team ~= wieldingPlayer.Team or Mob.Team then --
1 Like

:sweat_smile: :sweat_smile: :sweat_smile: Probably

local weapon = script.Parent
local dmg = 50 -- Change the damage value here
local hitsound = script.Parent.Parent.Handle.hitsound


weapon.Touched:connect(function(part)
	if part.Parent:findFirstChild("Humanoid") then
		local humanoid = part.Parent:findFirstChild("Humanoid")
		local character = humanoid.Parent
		local hitPlayer = game.Players:GetPlayerFromCharacter(character)
		local wieldingPlayer = game.Players:FindFirstChild(weapon.Parent.Parent.Name)
		if hitPlayer then
			if hitPlayer.Team ~= wieldingPlayer.Team then
				humanoid:TakeDamage(dmg)
				hitsound:Play()
				script:Destroy()
		else
			if humanoid:FindFirstChild("Mob") then
				humanoid:TakeDamage(dmg)
				hitsound:Play()
				script:Destroy()
			end
		end
	end
end)

Thank you. So many good suggestions. Let me give these a try. Be back soon.

What if I have multiple different monsters.

local Mob = game.Workspace:FindFirstChild(Reference of a Model with humanoid here)

Referencing each model wouldn’t work in a single variable. I would need a way to group all the monsters into one variable. local Mob =

You should give each mob a different way to identify it, that way different mobs could be dealt different damages.

I think you should set a team for the mobs rather than checking if they have a certain part which allows them to take damage.

I actually don’t know how to set monsters into teams. I was searching for a way to do that and came up with nothing.

Detect first if is touching player or non-player if non-player then check if it is monster, and if touching player check the team

Going to give you the solution because you were the closest to leading me to end result which was changing the line.

if hitPlayer.Team ~= wieldingPlayer.Team then

I changed the humanoid display name to Mob, then hid the display name from viewer. I did this to all the monsters and then changed the line to.

if humanoid.DisplayName == "Mob" or hitPlayer.Team ~= wieldingPlayer.Team then

Boom, feels good to get that fixed.

thanks.