How would I accurately compare values of a table to another tables values?

To answer your main question, your current script detects if the first value is the same in both tables.

Why is this happening? The simplest way to answer is that functions can return multiple values, but operators can accept only one on each side (some only on one). == is the equality operator and it checks if the value (not values) on the left is equal to the one on the right and returns a boolean.

unpack(tab) is a function which “unpacks” the array components of a given table into returned values. As said before, since functions return multiple values, there are cases where some aren’t required. Since == only accepts one value on each side, while your unpack of the table returns 3 values, it will take the first returned value from each. This means that instead of checking whether the 1st, 2nd and 3rd values of the tables are equal, it will only check the 1st.

Here’s a simple example:

local func1 = function ()
    return 1,2,3
end
local func2 = function()
    return 1,4,5
end
print(func1()==func2()) --> true because it never got 2, 3, 4 or 5

To fix this, here’s a simple check:

local function CheckTableEquality(t1,t2)
    for i,v in next, t1 do if t2[i]~=v then return false end end
    for i,v in next, t2 do if t1[i]~=v then return false end end
    return true
end

Here’s a slightly faster array variant:

local function CheckArrayEquality(t1,t2)
    if #t1~=#t2 then return false end
    for i=1,#t1 do if t1[i]~=t2[i] then return false end end
    return true
end

As for the actual anti-cheat method. I don’t exactly know what you are doing, but just the fact that you need to check validity of something on the client is already a red flag. Any sensitive information such as scores or inventory items should be stored 100% on the server. The client should just have a dumbed down simulation of what would happen if they had those items to avoid desyncs. Inconsistencies can then be checked through sent remotes when the player wants to interact with something.

17 Likes