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

Check if a string is not part of an array?

Asked by 6 years ago

For some reason this code doesn't work. Is there another way?

It's supposed to do the 'stuff' to everything (from i, v in pairs) as long as it's name is not whatever is in the list.

if v.Name ~= {"PrimaryPart", "Head", "HumanoidRootPart"} then
    -- stuff
end

1 answer

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

Well what your doing is comparing a string to a table, if you want to check if a string is inside a table we will have to go through each one and check individually

local t = {"PrimaryPart", "Head", "HumanoidRootPart"}

local checkname="string"

local isin=false
for _,v in pairs(t) do
    if v==checkname then
        isin=true
    end
end
if isin==true then
    print('run')
end

Here I set up a variable and then made a for loop to go through each string in the table, if the current string is the same as the string we are checking it makes a bool variable true.

Ad

Answer this question