Damaging team members

Hello, I made a script to damage players in a radius around a pet.
Now I want to check to see if a player is on their team before damaging them and I plan to make the healing pet the same way in reverse.

The problem is that when I try to check for a team member the script totally breaks and doesn’t damage anything or have an error message.

The three teams are Pithons, Algebros, and Mathletes.

Here is a portion of the original script, the rest is just adjustments based on player level:

local cloud = script.Parent
local ownername = ""
local char = script.Parent.Parent.Parent.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
local level = plr.leaderstats.Level.Value

if level >= 27 and level < 29 then
while true do
    for i, v in pairs(game.Players:GetPlayers()) do
         if v.Name == ownername then return end
		 local hrp = v.Character:FindFirstChild('HumanoidRootPart')
         if hrp then
           	local dist = (cloud.Position - hrp.Position).Magnitude
            if dist < 10 and script.Parent.Parent.Parent.Parent ~= hrp.Parent then -- in studs
               	if hrp.Parent.Humanoid.Health > 1 then 
				hrp.Parent.Humanoid.Health = hrp.Parent.Humanoid.Health  - 1-- do damage here					
				end
           	end
         end
    end
    wait(.1)
end
end

Here is what I added to try to get it to identify teams:

local cloud = script.Parent
local ownername = ""
local char = script.Parent.Parent.Parent.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
local level = plr.leaderstats.Level.Value

if level >= 27 and level < 29 then
while true do
    if plr.Team == "Algebros" then
    for i, v in pairs(game.Teams.Mathletes:GetPlayers()) do
         if v.Name == ownername then return end
		 local hrp = v.Character:FindFirstChild('HumanoidRootPart')
         if hrp then
           	local dist = (cloud.Position - hrp.Position).Magnitude
            if dist < 10 and script.Parent.Parent.Parent.Parent ~= hrp.Parent then -- in studs
               	if hrp.Parent.Humanoid.Health > 1 then 
				hrp.Parent.Humanoid.Health = hrp.Parent.Humanoid.Health  - 1-- do damage here					
				end
           	end
         end
    end
 for i, v in pairs(game.Teams.Pithons:GetPlayers()) do
         if v.Name == ownername then return end
		 local hrp = v.Character:FindFirstChild('HumanoidRootPart')
         if hrp then
           	local dist = (cloud.Position - hrp.Position).Magnitude
            if dist < 10 and script.Parent.Parent.Parent.Parent ~= hrp.Parent then -- in studs
               	if hrp.Parent.Humanoid.Health > 1 then 
				hrp.Parent.Humanoid.Health = hrp.Parent.Humanoid.Health  - 1-- do damage here					
				end
           	end
         end
    end
 elseif plr.Team == "Mathletes" then
    for i, v in pairs(game.Teams.Algebros:GetPlayers()) do
         if v.Name == ownername then return end
		 local hrp = v.Character:FindFirstChild('HumanoidRootPart')
         if hrp then
           	local dist = (cloud.Position - hrp.Position).Magnitude
            if dist < 10 and script.Parent.Parent.Parent.Parent ~= hrp.Parent then -- in studs
               	if hrp.Parent.Humanoid.Health > 1 then 
				hrp.Parent.Humanoid.Health = hrp.Parent.Humanoid.Health  - 1-- do damage here					
				end
           	end
         end
    end
 for i, v in pairs(game.Teams.Pithons:GetPlayers()) do
         if v.Name == ownername then return end
		 local hrp = v.Character:FindFirstChild('HumanoidRootPart')
         if hrp then
           	local dist = (cloud.Position - hrp.Position).Magnitude
            if dist < 10 and script.Parent.Parent.Parent.Parent ~= hrp.Parent then -- in studs
               	if hrp.Parent.Humanoid.Health > 1 then 
				hrp.Parent.Humanoid.Health = hrp.Parent.Humanoid.Health  - 1-- do damage here					
				end
           	end
         end
    end
elseif plr.Team == "Pithons" then
    for i, v in pairs(game.Teams.Algebros:GetPlayers()) do
         if v.Name == ownername then return end
		 local hrp = v.Character:FindFirstChild('HumanoidRootPart')
         if hrp then
           	local dist = (cloud.Position - hrp.Position).Magnitude
            if dist < 10 and script.Parent.Parent.Parent.Parent ~= hrp.Parent then -- in studs
               	if hrp.Parent.Humanoid.Health > 1 then 
				hrp.Parent.Humanoid.Health = hrp.Parent.Humanoid.Health  - 1-- do damage here					
				end
           	end
         end
    end
 for i, v in pairs(game.Teams.Mathletes:GetPlayers()) do
         if v.Name == ownername then return end
		 local hrp = v.Character:FindFirstChild('HumanoidRootPart')
         if hrp then
           	local dist = (cloud.Position - hrp.Position).Magnitude
            if dist < 10 and script.Parent.Parent.Parent.Parent ~= hrp.Parent then -- in studs
               	if hrp.Parent.Humanoid.Health > 1 then 
				hrp.Parent.Humanoid.Health = hrp.Parent.Humanoid.Health  - 1-- do damage here					
				end
           	end
         end
    end
  end
    wait(.1)
end
end

I also tried changing “if v.Name == owernname then return end” to “if v.Team == Mathletes then retrun end” but nothing works. I’m sure there is an easier way to do this because it took a 100 line script and made it into 750 lines…

1 Like
while true do
    if plr.Team == "Algebros" then
    for i, v in pairs(game.Teams.Mathletes:GetPlayers()) do
         if v.Name == ownername then return end
		 local hrp = v.Character:FindFirstChild('HumanoidRootPart')
         if hrp then
           	local dist = (cloud.Position - hrp.Position).Magnitude
            if dist < 10 and script.Parent.Parent.Parent.Parent ~= hrp.Parent then -- in studs
               	if hrp.Parent.Humanoid.Health > 1 then 
				hrp.Parent.Humanoid.Health = hrp.Parent.Humanoid.Health  - 1-- do damage here					
				end
           	end
         end
    end

You are not putting a wait() in this loop. Also, you do not have to put wait(.1), you can use wait() instead. It will make it faster without crashing.

You can actually use while wait() do instead of while true do to also get the same results. You can change it to:

while wait() do
    if plr.Team == "Algebros" then
    for i, v in pairs(game.Teams.Mathletes:GetPlayers()) do
         if v.Name == ownername then return end
		 local hrp = v.Character:FindFirstChild('HumanoidRootPart')
         if hrp then
           	local dist = (cloud.Position - hrp.Position).Magnitude
            if dist < 10 and script.Parent.Parent.Parent.Parent ~= hrp.Parent then -- in studs
               	if hrp.Parent.Humanoid.Health > 1 then 
				hrp.Parent.Humanoid.Health = hrp.Parent.Humanoid.Health  - 1-- do damage here					
				end
           	end
         end
    end
2 Likes

(assuming ‘ownername’ is the name of the player that owns the pet)
i would do something along these lines; the script isn’t finished just meant to give you an idea

-- put in the variable declarations
local plrowner = game.Players:FindFirstChild(ownername)
local ownerteam = plrowner and plrowner.Team or nil

-- put in the while true loop
for i, v in pairs(game.Players:GetPlayers()) do
    if v.Team ~= ownerteam then
        -- put the code that damages the plrs
    end
end

Instead of adding a seperate if statement for each team, just add an OR teams are the same check to the if statement that is checking to see if the player is the same as the pet’s owner.

TheNickmaster21Programmer
Instead of adding a seperate if statement for each team, just add an OR teams are the same check to the if statement that is checking to see if the player is the same as the pet’s owner.

Yes! I don’t know why I didn’t think of that.
Thank you.

TheziProgrammer
local ownerteam = plrowner and plrowner.Team or nil

I don’t understand what you did there with defining a variable with and. I have never seen that.
Do you have a link on that?

GamingBroCoolkid328
You are not putting a wait() in this loop. Also, you do not have to put wait(.1) , you can use wait() instead. It will make it faster without crashing.

I put the wait(.1) there so it will do 1 damage, for example, every 0.1 seconds.

I’m sorry but I messed up the indenting when trying to copy/paste it so it doesn’t look right.
The wait(.1) goes for the while true do loop.
Do for loops need a wait?

Yes and no. It depends on how much you are looping through. If you made a loop that will run 3000 times you might want to add a wait because that could cause a lot of lag. I was making a loop and had it run a high amount of times and studio almost crashed… :smile: