Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to keep zombies from killing one another if they have a certain name?

I'm making a minigame where two teams of three control zombies with swords in an arena by stepping on buttons. The objective of the minigame is to eliminate the other team. However, it's possible for members of the same team to kill each other's zombies. The name of each of the zombies are RedTeam, RedTeam2, and RedTeam3, along with GreenTeam, GreenTeam2, and GreenTeam3 for the other team. I want to edit the sword script so if the zombies have a certain name they cannot be killed. Here is the killing script for the swords the zombies carry:

function onTouched(hit)
	local human = hit.Parent:findFirstChild("Zombie")
	if (human ~= nil) then
		human.Health = human.Health - 10
	end
end

--if (script.Parent ~= nil) and (script.Parent.className == "Part") then --Work if in a block
--	connection = script.Parent.Touched:connect(onTouched)
--end
script.Parent.Touched:connect(onTouched)

2 answers

Try using this:

function onTouched(hit)
    local human = hit.Parent:findFirstChild("Zombie")
    if (human ~= nil) then
        human.Health = human.Health - 10
if hit.Parent.Name == GreenTeam or GreenTeam2 or GreenTeam3 then
	human.Health = human.Health - 0
    end
end

--if (script.Parent ~= nil) and (script.Parent.className == "Part") then --Work if in a block
--  connection = script.Parent.Touched:connect(onTouched)
--end
script.Parent.Touched:connect(onTouched)

You can switch green to red to put in the other team's swords.

A question you have to answer is how you will determine what team the player is on. One solution if you are using teams in game.Teams:

local greenTeam = game.Teams.Green -- change to the name of your green team
local greenTeamName = "GreenTeam" -- name that all green zombies start with
local redTeamName = "RedTeam" -- name that all red zombies start with
local redTeam = game.Teams.Red -- change to the name of your red team
local function ZombieOnOurTeam(model)
	-- Judge whether the zombie is on our team by their name. Figure out our team by looking at LocalPlayer.Team:
	local teamName = game.Players.LocalPlayer.Team == greenTeam and greenTeamName or redTeamName -- this will set teamName to "GreenTeam" if the player's team equals the greenTeam variable or "RedTeam" if it isn't
	return model.Name:find(teamName) == 1 -- true if the zombie's name starts with whatever teamName is (either "GreenTeam" or "RedTeam")
end

local debounce = false -- I have added debounce so that damage can only be dealt once per frame, rather than once for every part you hit. You can delete all references to 'debounce' and the 'wait()' command if you prefer how it worked before.
local function onTouched(hit)
	if debounce or not hit.Parent then return end
	local h = hit.Parent:FindFirstChild("Zombie")
	if not h then return end -- only continue if it's a zombie
	if ZombieOnOurTeam(hit.Parent) then return end -- do not continue if it's a zombie that is on our team
	debounce = true
        h:TakeDamage(10)
	wait()
	debounce = false
end
script.Parent.Touched:Connect(onTouched)

Note: findFirstChild and connect are deprecated (ie may be removed in the future); use the capitalized versions instead (FindFirstChild and `Connect).

If you are using a custom team system (not game.Teams), then you will need to change the ZombieOnOurTeam function and the variables before it.

This discussion is preserved as it was. New answers, comments, and votes are closed.