I have a script that belongs to a boss-fighting game and inside the map is lava. I want to make certain people immune to the scorching death of the lava. For instance, the bosses, I dont want them to die in the lava or else it would be a tremendously easy game. So to account for this I wrote this piece of code.
function onTouched(part) local h = part.Parent:findFirstChild("Humanoid") if part.Parent.Name ~= "Skelters" or part.Parent.Name ~= "Captain Stellus" or part.Parent.Name ~= "Lt. Brains" and h == nil then h.Health = 0 end end script.Parent.Touched:connect(onTouched)
It seems to kill everyone. And it does not give any errors.
Thanks in advance, Radio
function onTouched(part) local h = part.Parent:findFirstChild("Humanoid") if part.Parent.Name ~= "Skelters" and part.Parent.Name ~= "Captain Stellus" and part.Parent.Name ~= "Lt. Brains" and h ~= nil then --Change this h.Health = 0 end end script.Parent.Touched:connect(onTouched)
== means 'is equal to', and you don't want h to equal nil, ~= means 'not equal to', so you want to say and h is not equal to nil.
Also I changed your 'or's to 'and's, it is just how I do it, I am not sure if or is proper.
Hope I helped :D
You just made a logic error. Say it out loud.
if part.Parent.Name ~= "Skelters" or part.Parent.Name ~= "Captain Stellus" or part.Parent.Name ~= "Lt. Brains"
To pass the condition, the name only has to "not equal" one of these strings. It's the same as saying this: "If I'm out of cereal OR if I'm out of milk, I'll go to the store." I don't need to be out of both; I only need to be out of one.
You're doing the same thing. "If the name isn't Skelters OR if the name isn't Captain Stellus, do this." Let's say the name is indeed Skelters. The condition will still pass, because it isn't Captain Stellus.
What you want to do is check if the name isn't Skelters AND it isn't Captain Stellus.
if part.Parent.Name ~= "Skelters" and part.Parent.Name ~= "Captain Stellus" and part.Parent.Name ~= "Lt. Brains"
I also don't know why you have h == nil
. Perhaps you made a typo? You want to make sure the humanoid does exist.
if part.Parent.Name ~= "Skelters" and part.Parent.Name ~= "Captain Stellus" and part.Parent.Name ~= "Lt. Brains" and h ~= nil then
You're going in the right direction, just a couple of things wrong with your if-statement.
if part.Parent.Name ~= "Skelters" or part.Parent.Name ~= "Captain Stellus" or part.Parent.Name ~= "Lt. Brains" and h == nil then
What you've written, and what you're going for are two different things. First off, you want to make sure that you don't die if the character has ANY of those names, and so you shouldn't be using "or" between those, rather:
if part.Parent.Name ~= "Skelters" and part.Parent.Name ~= "Captain Stellus" and part.Parent.Name ~= "Lt. Brains"
That's what you're looking for.
As far as your "h==nil" is concerned, you have it backwards. You want to make sure that the humanoid IS present. Also, I would put that in an if-statement prior to looking for the names, so as to save your computer some time checking through the names.
if h ~= nil then if part.Parent.Name ~= "Skelters" and part.Parent.Name ~= "Captain Stellus" and part.Parent.Name ~= "Lt. Brains"
As an aside, I would use a different method so as to keep your code clean in the future: Use an array alongside a loop to check for names, rather than just one large if-statement:
local Bosses = {"Skelters", "Captain Stellus", "Lt. Brain"}; script.Parent.Touched:connect(function(part) local h = part.Parent:findFirstChild("Humanoid"); if(h) then local ItsABoss = false; for i, v in pairs(Bosses) do if(part.Parent.Name == v) then ItsABoss = true; break; end end if(ItsABoss == false) then h.Health = 0; end end
This way, you can add bosses to the table "Bosses" easy-peasy.
Hope that helps!