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

How can I insert a value with a bool value into a table?

Asked by 6 years ago
Edited 6 years ago

Alright I've got a table of players that are banned for my game

local banned = {["Player1"] = true,["LucarioZombie"] = true}

I've made an anti-exploit script and I can't seem to add a player to the ban list. I've tried so many methods. So many errors. I just cant add the bool value along with the players name. Say that I wish to add Telamon to the ban list and try by

table.insert(banned,"Telamon" = true)

table.insert(banned,["Telamon"] = true)

table.insert(banned,["Telamon" = true])

table.insert(banned,"Telamon", true)

table.insert(banned,"Telamon" == true)

banned = banned + {["Telamon"] = true}

But that only adds the player name without the true/false value. My checker goes by

if banned[player.Name] == true then
    player:Kick("Ha! Loser")
end

Say that I wish to add Telamon to the ban list and try by

How can I turn my array banned from this;

local banned = {["LucarioZombie"] = false}

to

local banned = {["LucarioZombie"] = false, ["Telamon"] = true}

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

I think what you're looking for is how to make a dictionary and how to use table.insert

https://www.robloxdev.com/articles/Table#Dictionaries

Dictionaries use keys rather than indices, which means they can't be sorted like normal tables.

This would be a valid dictionary:

local banned = {
    ["Builderman"] = false,
    ["Telamon"] = true
}

But inserting using table.insert would not be easy. I'll try to walk through why each attempt was not successful.

1 - table.insert(banned,"Telamon" = true)

2 - table.insert(banned,["Telamon"] = true)

1 and 2 are the same, but 1 uses sugar syntax which is possible for your convenience. Table.insert has two parameters: the table, and the value you are inserting. Telamon=true is a statement, so you unfortunately can't insert it as if it were a value.

3 - table.insert(banned,["Telamon" = true])

This isn't exactly when you would want square brackets (not that you necessarily need them thanks to sugar syntax)

4 - table.insert(banned,"Telamon", true)

This sends three values when table.insert only has two parameters.

5 - table.insert(banned,"Telamon" == true)

== is for checking for a condition. You can think of it like this: x = 1 means "x equals 1" (you're setting it), while if x == 2 then reads "if x is equal to 2 then" (you're checking it)

6- banned = banned + {["Telamon"] = true}

It appears that this is an attempt to concatenate a table with a dictionary, which wouldn't exactly work. String concatenation such as string1 .. string2 works, and arithmetic such as number1 + number2 works, but this would not.

So instead, here's a method that would work:

local banned = {"Builderman", "Telamon", "Shedletsky"}

--If you want to print everyone in the list, you can use a generic (non numeric) for loop:

function listBannedPlayers()
    print("Banned players:");
    for i,v in pairs(banned) do
        print(v) -- Prints Builderman, then Telamon, then Shedletsky
    end
end
-- If you want to add someone, you can simply do this:

table.insert(banned,"Sorcus")

-- If you want to remove someone, you can simply iterate through it, check if the value equals their name, and remove them from the position

function unbanPlayer(username)
    for i = 1,#banned do
        if banned[i] == username then
            table.remove(banned, i)
        end
    end
end

-- If you want to check if a player is banned, you can do:

function checkBanned(username)
    local isBanned = false -- default to false
    for i,v in pairs(username) do
        if v == username then -- if it found the player's name in the list
            isBanned = true
        end
    end
    return isBanned -- return true or false
end

-- which would let you do:
if checkBanned(player.Name) then
    player:Kick("You're banned");
end

-- To test it:
listBannedPlayers()
unbanPlayer("Sorcus")
unbanPlayer("Shedletsky")
table.insert(banned,"1x1x1x1")
listBannedPlayers()

If you have any questions comment below, if this was helpful please mark it correct or upvote Hope this helps!

0
I can confirm that works and can also confirm that your answer is detailed and I understood it. You're a god. ( I love how you picked up on banning admins xd ) LucarioZombie 291 — 6y
Ad

Answer this question