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

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

Asked by
Adryin 120
9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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)
0
if part.Name == "Rock1" or part.Name == "Rock2" and part:IsA("Model") then --code end aquathorn321 858 — 9y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

"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.

Ad

Answer this question