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

How to add a player to a table?

Asked by 9 years ago
speakers =  {"Awesomespyhacker12"}
banned =  {}

function checkSpeakers(name)
    for i,v in pairs(speakers) do
        if (string.upper(name) == string.upper(v)) then return true end
    end
   return false
end

function banPlayer(banner, victim)
    if (victim ~= banner) then
        victim:Remove()
        banned[victim.Name] = victim.Name
    end
end

function matchPlayer(str)
    local result = nil
    local players = game.Players:GetPlayers()
    for i,v in pairs(game.Players:GetPlayers()) do
 if (string.find(string.lower(v.Name), str) == 1) then
            if (result ~= nil) then return nil end
            result = v
        end
    end
    return result
end

function onChatted(msg, recipient, speaker)
    local source = string.lower(speaker.Name)
    msg = string.lower(msg)
    if (string.find(msg, "ban/") == 1) then 
        for word in msg:gmatch("%w+") do 
            local p = matchPlayer(word)
            if (p ~= nil) then
                banPlayer(speaker, p)
            end
        end
    end
end

function onPlayerEntered(newPlayer)
    for i,v in pairs(banned) do
        if (v:lower() == newPlayer.Name:lower()) then
            newPlayer:Remove()
        end
    end
    if checkSpeakers(newPlayer.Name) then
        newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
    end
end
game.Players.PlayerAdded:connect(onPlayerEntered)

So I am using this banning script. It works and removes the player but it won't add the said banned person to the table so they can't join back. Any help is gratefully appreciated. Thanks! Regards, Awesomespyhacker12

1 answer

Log in to vote
0
Answered by 9 years ago

You didn't add p to the table

speakers =  {"Awesomespyhacker12"}
banned =  {}

function checkSpeakers(name)
    for i,v in pairs(speakers) do
        if (string.upper(name) == string.upper(v)) then return true end
    end
   return false
end

function banPlayer(banner, victim)
    if (victim ~= banner) then
        victim:Remove()
        banned[victim.Name] = victim.Name
    end
end

function matchPlayer(str)
    local result = nil
    local players = game.Players:GetPlayers()
    for i,v in pairs(game.Players:GetPlayers()) do
 if (string.find(string.lower(v.Name), str) == 1) then
            if (result ~= nil) then return nil end
            result = v
        end
    end
    return result
end

function onChatted(msg, recipient, speaker)
    local source = string.lower(speaker.Name)
    msg = string.lower(msg)
    if (string.find(msg, "ban/") == 1) then 
        for word in msg:gmatch("%w+") do 
            local p = matchPlayer(word)
            if (p ~= nil) then
        table.insert(banned, p.Name) -- table.insert inserts the string value p.Name to the table :D
                banPlayer(speaker, p)

            end
        end
    end
end

function onPlayerEntered(newPlayer)
    for i,v in pairs(banned) do
        if (v:lower() == newPlayer.Name:lower()) then
            newPlayer:Remove()
        end
    end
    if checkSpeakers(newPlayer.Name) then
        newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
    end
end
game.Players.PlayerAdded:connect(onPlayerEntered)

Ad

Answer this question