Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

script ignoring if statement but doing function?

Asked by
Donut792 216 Moderation Voter
5 years ago

alright for this script right here the explosion does print the parts it hit that are not locked but it also runs the if statement even when the basePart.Parent.Name does not equal Blue or Red it also destroys anything that is within a model and give a parent is nil value error and workspace is locked error

script:

workspace.ChildAdded:Connect(function(obj)
    if obj:IsA("Explosion") then
        obj.Hit:Connect(function(basePart, distance)
            print(basePart.Name)
            if basePart.Parent.Name == "BlueBarrel" or "RedBarrel" then
                basePart.Parent:Destroy()
            end
        end)
    end
end)

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

What you do in your code is a very common mistake new developers do when using "if" statements.

The thing with Lua (could apply to other languages as well) is that you need to provide what it should check.

Basically in your if statement here:

if basePart.Parent.Name == "BlueBarrel" or "RedBarrel" then

you check if the basePart's parent name is "BlueBarrel" or if "RedBarrel" exists. Since "RedBarrel" is not falsy (AKA. false, nil etc.), but exists, this will return true and the if statement will return true.

You probably thought that it would check if the basePart's parent name was "BlueBarrel" or "RedBarrel".

There is not much you have to change though:

if basePart.Parent.Name == "BlueBarrel" or basePart.Parent.Name == "RedBarrel" then
    -- Continue
end
0
oh alright thank you so much man i would pay you if i could Donut792 216 — 5y
0
Don't worry. No need to pay, I like helping people and seeing them getting happy when I help them is more than enough reward to me. 1TheNoobestNoob 717 — 5y
0
you can pay him in thankfulness lol aazkao 787 — 5y
Ad

Answer this question