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

I am trying to make a ban player with a string, how can I do this?

Asked by 4 years ago

I am looking for a script that can use a string like "local Banned = {}"

If you can see this, then please help.

My script: V

local Banned = {"CTGM_DEV";
"OtherPLR"}

game.Players.PlayerAdded:Connect(function(plr)
    if Banned[plr.Name] then
        plr:Kick("Banned from this game.")
    end
end)
0
use a comma not a semicolon probably... greatneil80 2647 — 4y
0
That won't be enough in this case. That would make it a valid Lua array, but the player names would be the values for indices 1 and 2, not keys as he's expecting. Gullet's solution is how it should be done. EmilyBendsSpace 1025 — 4y

2 answers

Log in to vote
3
Answered by
gullet 471 Moderation Voter
4 years ago

You'll want to use Player.UserId instead as this can not change, unlike usernames. To create a table which you can check the way you do, you need to assign them true (or truthy) values.

local banned = {
    [userid_1] = true,
    [userid_2] = true
}

game:GetService("Players").PlayerAdded:Connect(function(plr)
    if banned[plr.UserId] then
        plr:Kick("Banned from this game.")
    end
end)
Ad
Log in to vote
1
Answered by
Ankur_007 290 Moderation Voter
4 years ago
Edited 4 years ago

The answer @gullet has sent is absolutely right and will suffice your problem. I'm just going to explain to you why your script didn't work in the first place.

The problem isn't using semicolons over commas, it's your table's content itself. Let's first discuss some essentials

Dictionaries


A lot of people take use different analogies to explain the concept of dictionaries, but I'll use a very straightforward analogy. Imagine a dictionary in Lua as an actual dictionary. You have lots of words each with their own meaning, the words can be verbs, adjectives, anything and their meanings can be of any form as well. To get the meaning of a word, you look up the word in the dictionary and find it's meaning next to it. The implementation of this in Lua is as such:

local myDictionary = {
    Word = "Meaning",
    OtherWord = "OtherMeaning"
}

print(myDictionary.Word) --> Meaning
print(myDictionary.OtherWord) --> OtherMeaning

myDictionary.NewWord = "NewMeaning"

print(myDictionary.NewWord) --> NewMeaning

So we observe that:

1• Dictionaries always include pairs of values

Some technical points:

1• In the example, each word is an index — and index is what you look up and each meaning is a value 2• Indices (indexes) can be of any DataType, number, bool, string and even instances!

Conventions and stuff:

1• If more than one pair of entries are present in a dictionary, break your dictionary into several lines for readability 2• Often dictionary indices are enclosed by rectangular rackets. This is necessary in certain cases such as numerical indices

Tables as dictionaries


Tables can be thought of as dictionaries with numerical indices, i.e.:

local myTable = {"Value1", "Value2"}
print(myTable[1]) --> Value1
-- same as

local myDictionary = {
    [1] = "Value1",
    [2] = "Value2"
}
print(myDictionary[1]) --> Value1

Now that we know what's what, we can see that Banned[plr.Name] looks up for a value in Banned that is paired with plr.Name as it's index, but our banned table by default has numerical indices! Meaning our value is supposed to be an index!

That's it! After that, @gullet's answer is what you need read.

Answer this question