[lua] Roblox Admin Command Script

I'm currently trying to make a new admin command script; all I have so far is the kill command... everything I've tried (so far) works unless I use the ":* me" parameter ("*"being any command and ":" being the recognition character. I don't quite understand why this doesn't work.

I've tried a few crazy things to try to make this work, so the code may be butchered from what I had it as originally...

admins = {"FakeNameHereSoNoStalkers"}

function kill(target)
    for i=1,#target do
        game.Players.target[i].Character:BreakJoints()
    end
end

function isadmin(source)
    for i=1,#admins do
        if admins[i]:lower()==source:lower() then return true end
    end
end

function findplayer(msg, source)
    people = {}
    c = game.Players:GetChildren()
    if msg:lower()=="me" then
        table.insert(people, source)
        return people
    elseif msg:lower()=="all" then
        for i=1,#c do
            table.insert(people, c[i])
        end
        return people
    else
        local length = msg:len()
        for i=1,#c do
            if c[i].Name:lower():sub(1, length)==msg:lower() then
                table.insert(people, c[i])
            end
        end
        return people
    end
end

game.Players.PlayerAdded:connect(function(player)
    source = player.Name
    if isadmin(source) == true then
        player.Chatted:connect(function(msg, player)
            if msg:lower():sub(1,6)==":kill " then
                msg = msg:sub(7)
                target = findplayer(msg, source)
                kill(target)
            end
        end)
    end
end)

This question is related to lua roblox

The answer is


for i=1,#target do
    game.Players.target[i].Character:BreakJoints()
end

Is incorrect, if "target" contains "FakeNameHereSoNoStalkers" then the run code would be:

game.Players.target.1.Character:BreakJoints()

Which is completely incorrect.


c = game.Players:GetChildren()

Never use "Players:GetChildren()", it is not guaranteed to return only players.

Instead use:

c = Game.Players:GetPlayers()

if msg:lower()=="me" then
    table.insert(people, source)
    return people

Here you add the player's name in the list "people", where you in the other places adds the player object.


Fixed code:

local Admins = {"FakeNameHereSoNoStalkers"}

function Kill(Players)
    for i,Player in ipairs(Players) do
        if Player.Character then
            Player.Character:BreakJoints()
        end
    end
end

function IsAdmin(Player)
    for i,AdminName in ipairs(Admins) do
        if Player.Name:lower() == AdminName:lower() then return true end
    end
    return false
end

function GetPlayers(Player,Msg)
    local Targets = {}
    local Players = Game.Players:GetPlayers()

    if Msg:lower() == "me" then
        Targets = { Player }
    elseif Msg:lower() == "all" then
        Targets = Players
    elseif Msg:lower() == "others" then
        for i,Plr in ipairs(Players) do
            if Plr ~= Player then
                table.insert(Targets,Plr)
            end
        end
    else
        for i,Plr in ipairs(Players) do
            if Plr.Name:lower():sub(1,Msg:len()) == Msg then
                table.insert(Targets,Plr)
            end
        end
    end
    return Targets
end

Game.Players.PlayerAdded:connect(function(Player)
    if IsAdmin(Player) then
        Player.Chatted:connect(function(Msg)
            if Msg:lower():sub(1,6) == ":kill " then
                Kill(GetPlayers(Player,Msg:sub(7)))
            end
        end)
    end
end)