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

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

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)

3 comments

0

Everything in the script looks fine to me, is there any errors in the output?

0

Just found a problem tho, is the Particles inside the script or not? (Line 06 - Line 07)

0

Yes it all works fine, it only brakes when adding the or.

1 answer

✓ Accepted answer

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!

1 comment

0

Oh that makes sense, Thank you it works perfectly now!

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