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

How do I make the if statement find Rock1 or Rock2?

I want to make it so if the Model's name is Rock1 or Rock2 it will print yey. I do not want to use two if statements, so can you please tell me whats wrong in line 3?

area = {1,2}
game.Workspace.ChildAdded:connect(function(part)
	if part.Name  == "Rock"..#area and part:IsA("Model") then
		print("yey")
	end
end)

1 comment

0

if part.Name == "Rock1" or part.Name == "Rock2" and part:IsA("Model") then --code end

1 answer

"Rock" .. #area is just "Rock2".

If you mean "Rock" and then a number...

	if part.Name:match("Rock%d+") == part.Name then
		-- %d means digit
		-- %d+ means at least one digit
		-- :match returns the "longest" result that is the thing
		-- so we need it to be the *whole* thing, which is `part.Name`

It might be simpler to just say "begins with "Rock"":

	if part.Name:sub(1, 4) == "Rock" then

If you mean the name is contained in the list rockNames, then you would have a loop (or a function which uses the loop, which I'll do here)

function contains(tab, element)
	for _, value in pairs(tab) do
		if value == element then
			return true
		end
	end
end


.....
	if contains( rockNames, part.Name) then


Overall, it would be better to use a consistent name/detection method for all "Rock" objects (if not name, then for instance :FindFirstChild("Rock"))

They are all Rocks. If you want to give them a number, use a NumberValue or IntValue object. etc.

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