Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
3

How do I add a player to a table in a script when a TextBox contains their name?

Asked by 5 years ago

I have a script for manually putting a player in the banlist in ROBLOX Studio, but I'm wondering how I could add people to the banlist in-game using a TextBox.

local BannedList = {
    ["PlayerX"] = true;
    ["PlayerY"] = true;
    ["PlayerZ"] = true;
}

function NewPlayer(pl)
    if BannedList[pl.Name] then
        wait()
        pl:Kick();
    end
end

game.Players.PlayerAdded:connect(NewPlayer)
table.foreach(game.Players:GetPlayers(), function(i,v) NewPlayer(v) end)

2 answers

Log in to vote
1
Answered by
woodengop 1127 Moderation Voter
5 years ago

First thing to do is Access the players by using the method, :GetPlayers.

local players=game.Players:GetPlayers()

Now that only access the Players, how do we actually access a certain player? By the pairs function.

local players=game.Players:GetPlayers()

script.Parent.FocusLost:connect(function()
    for i,v in pairs( players ) do
        if script.Parent.Text==v.Name then
            game.Players:WaitForChild(script.Parent.Text):Kick()
        end
    end
end)

The FocusedLost event fires when the current TextBox is unFocused(by pressing the Enter key)

1
kicking is easy-yet he wants to know how to ban. unmiss 337 — 5y
Ad
Log in to vote
1
Answered by
saenae 318 Moderation Voter
5 years ago

This might work;

table.insert(BannedList, #BannedList+1, TextBox.Text)

Just run that right after having gotten input from the textbox.

Answer this question