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

i want enemy.TreeHitBox:Destroy() to apply to both of them. But how?

ShellMouthNPC and ForestShellMouthNPC are both alive rn if i run that code it only destroys the object for the first one

if enemy.Name == "ShellMouthNPC" or enemy.Name == "ForestShellMouthNPC" then  --(Wave 8, 10)
            enemy.TreeHitbox:Destroy()    
            print("Attack Removed")
        end

6 comments

0

you only have it destroy one of them though, if you want to destroy both, you have to run it in a loop or have two if statements.

0

you could also use a bit of string manipulation to shorten your code

0

Do you have more code you could share? You could try doing this with a :FindFirstChild(enemy.Name) check.

0

what do youmean by string manipulation

0

What he meant by that is why are your strings so long?

0

if enemy.Name == "ShellMouthNPC" then --(Floor 8, 10) enemy.TreeHitbox:Destroy() enemy.Parent:FindFirstChild("ForestShellMouthNPC").TreeHitbox:Destroy() end

1 answer

What I meant by my comment was you need to have it one of two ways:

I removed the first one because you shouldn't use it and apparently including it, and telling you not to use it to show you what not to do is wrong?

Now the one you should use is this one, it will loop twice through the if statement you already have:

for i=1,2 do
	if enemy.Name == "ShellMouthNPC" or enemy.Name == "ForestShellMouthNPC" then --(Wave 8, 10)
		enemy.TreeHitbox:Destroy()    
		print("Attack Removed")
	end
end

This last one I didn't mention in my comment because it requires a bit more work, but it will be more helpful to you in the long run than the other two. Now you could get even more fancy and store the enemies in a table and make the for loop even more useful:


for i,v in pairs(enemy) do
	if v.Name == "ShellMouthNPC" or v.Name == "ForestShellMouthNPC" then
		v.TreeHitbox:Destroy()
		print("Attack Removed")
	end
end

i is the number of times the loop iterates (how many times it loops), and v would be each enemy inside the table called "enemies". With this way you could easily check for any property of each enemy by checking for v.Property where Property would be whichever property you want to check for. This also allows you to edit each enemy's property as well.

3 comments

0

i already have them stored in a table

0

if workspace.DungeonTemp:FindFirstChild("Wave1") then local tableOfEnemies = game.Workspace.DungeonTemp.Wave1:GetChildren() for _, enemy in pairs(tableOfEnemies) do

0

Ok then why didn't you put all of your code in your question then? Since my first example works with your current table, you should use that one, and also mark this as the accepted answer because it does answer your question correctly.

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