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

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

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:

1local banned = {
2    ["Builderman"] = false,
3    ["Telamon"] = true
4}

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:

01local banned = {"Builderman", "Telamon", "Shedletsky"}
02 
03--If you want to print everyone in the list, you can use a generic (non numeric) for loop:
04 
05function listBannedPlayers()
06    print("Banned players:");
07    for i,v in pairs(banned) do
08        print(v) -- Prints Builderman, then Telamon, then Shedletsky
09    end
10end
11-- If you want to add someone, you can simply do this:
12 
13table.insert(banned,"Sorcus")
14 
15-- 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
View all 47 lines...

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