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

Am I incorrectly using "Or" in my 'If Statement'?

Asked by
PastDays 108
5 years ago
Edited 5 years ago

If i remove 'or "Barrier"' it works but when i add it everything is setting the script off.

local enabled = true
function onTouched(hit)
    if enabled == true and
        hit.Name == "Object" or "Barrier" then
        enabled = false
        script.Parent.Particles.Rate = 250
            local Copy = script.Particles:Clone()
            Copy.Parent = hit
            Copy.Remover.Disabled = false
            wait(0.3)
        script.Parent.Particles.Rate = 0
        enabled = true
    end
end


Connection = script.Parent.Touched:Connect(onTouched)
0
Everything in the script looks fine to me, is there any errors in the output? AIphanium 124 — 5y
0
Just found a problem tho, is the Particles inside the script or not? (Line 06 - Line 07) AIphanium 124 — 5y
0
Yes it all works fine, it only brakes when adding the or. PastDays 108 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago

Yes you are. When you use or it is like you are creating an entirely new if statement(not quite, but close). Because strings "" evaluate to truthy in Lua your if statement will always pass because "Barrier" evaluates to true. What you need to do is have another hit.Name== after the or like so:

if hit.Name == "Object" or hit.Name == "Barrier" then
    -- code
end

Now the only things that will pass through the if statement are things named "Object" and things named "Barrier". I hope this helped, but if it did not then feel free to post a question in the comments below. Have a great day scripting!

0
Oh that makes sense, Thank you it works perfectly now! PastDays 108 — 5y
Ad

Answer this question