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

local variable table error?

Asked by 3 years ago
Edited 3 years ago

Hi, i'm making an Anti Cheat for anything but this isn't working:

local blacklisted_test = {'test1','test2'}

for _,v in pairs(game.Players.LocalPlayer.PlayerGui:GetChildren()) do if v.Name == blacklisted_test then v:Destroy() end end

How can i fix it?

0
Your table seems fine here. Please post your full source code as I don't see anything wrong with the code that is posted as of right now. TribotGamerGX 184 — 3y
0
oh sorry i wrote wrong. mainmodul 45 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

You forgot the unpack the table.

so what i did:

local blacklisted_test = {'test1','test2'} local Children = game.Players.LocalPlayer.PlayerGui:GetChildren()

for _, ItemNames in pairs(Children) do for _, ItemNames2 in pairs(blacklisted_test) do if ItemNames.Name == ItemNames2 then ItemNames:Destroy() end end end

Ad
Log in to vote
0
Answered by 3 years ago

To start off, I properly formatted your code.

local blacklisted_test = {'test1','test2'}

for _,v in pairs(game.Players.LocalPlayer.PlayerGui:GetChildren()) do
    if v.Name == blacklisted_test then 
        v:Destroy() 
    end
end

What you are doing wrong is checking the name of an object, a string, against a table. A string and a table are never the same, so this will always return false. An easy fix is to check if table.find(blacklisted_test, v.Name) then. This will go through the table, and return where it found the given item. Or if it does not exist within the table, it will return nil, which is a falsy value.

local blacklisted_test = {'test1','test2'}

for _,v in pairs(game.Players.LocalPlayer.PlayerGui:GetChildren()) do
    if table.find(blacklisted_test, v.Name) then 
        v:Destroy() 
    end
end

A more performant solution though, is to set all blacklisted items as the key, and true as value. Then you can index blacklisted_test[v.Name], and if that is true, it's blacklisted.

local blacklisted_test = {test1 = true, test2 = true}

for _,v in pairs(game.Players.LocalPlayer.PlayerGui:GetChildren()) do
    if blacklisted_test[v.Name] then 
        v:Destroy() 
    end
en

Answer this question