How do i make multiple and in script

Hello, i am trying to make if multiple and in a script. If something and something and multiple things then something happens. The issue is that when i try to put more than 2 and then it’s not working anymore. I’ve tried to figure it out, but i was unable to solve the problem.

I need the script for a buzzer system. If all buzzers are red(material is neon) then a fail sound will play. It will also check while true do if the buzzers are pressed.

-- The script that doesn't work
local Part1 = game.Workspace.Part1
local Part2 = game.Workspace.Part2
local Part3 = game.Workspace.Part3
local Part4 = game.Workspace.Part4
while true do
if Part1.Material == "Neon" and Part2.Material == "Neon" and Part3.Material == "Neon" and Part4.Material == "Neon" then
script.Parent.Sound:Play() -- All buzzers are pressed. It will play a fail sound
end

The only idea I could have is that instead of "Neon", try Enum.Material.Neon

Also I think you forgot to include the last end in the code you sent

2 Likes

Not sure if you just didn’t copy the whole thing, but you don’t have a task.wait() in the loop so it’s probably just timing out. When you add it, make sure it’s not in the if statement.

if Part1.Material == Enum.Material.Neon
-- Not
if Part1.Material == "Neon"

Also Anchor the Parts

I need like multiple and. 4 and, and then some function happens. It will also check while true do

Here is the full working script.

local Part1 = game.Workspace.Part1
local Part2 = game.Workspace.Part2
local Part3 = game.Workspace.Part3
local Part4 = game.Workspace.Part4
Part1.Anchored = true
Part2.Anchored = true
Part3.Anchored = true
Part4.Anchored = true
while true do
if Part1.Material == Enum.Material.Neon and Part2.Material ==  Enum.Material.Neon and Part3.Material == " Enum.Material.Neon and Part4.Material == Enum.Material.Neon then
script.Parent.Sound:Play()
end

Did it work?

local parts = {
	game.Workspace.Part1;
	game.Workspace.Part2;
	game.Workspace.Part3;
	game.Workspace.Part4;
}


while true do
	for i,part in pairs(parts) do
		part.Anchored = true
		if part.Material == Enum.Material.Neon then
			script.Parent.Sound:Play()
		end
	end
	task.wait()
end

Try this, more efficient and saves you time.

1 Like