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

Help on adding admins to a table with chatted?

Asked by 8 years ago

I am trying to use this but it won't work.

local Admins = {}

table.insert(Admins, Player.Name)

I am making admin commands so when i do !admin plr they can now say the commands, which doesn't work for me..

1 answer

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

I recommend you do something like the following:

local admins = {["EnchantedGamerOne"] = true} -- players who are admins
local prefix = "!" -- prefix, for use in getCommand()

local cmds = {} -- table of commands

cmds.admin = function(plr)
    admins[plr.Name] = plr
end)

function getCommand(msg)
    local pref = string.find(msg,prefix)
    if pref and pref == 1 then
        local nmsg = string.gsub(msg,prefix,"") -- removes it from the msg
        local cmd = string.match(nmsg,"(%w+) ") or string.match(msg,"(%w+)")
        cmd = string.lower(cmd)
        if cmd and cmds[cmd] then
            local args = {}
            while string.match(nsmg," ") do
                -- stuff for putting args in here, as well as converting them to numbers, players, etc
            end
            return cmd,args
        end
    end
    return nil
end

game.Players.PlayerAdded:connect(function(p) -- when a player is added
    p.Chatted:connect(function(msg,rec) -- when they chat
        if isAdmin(p) == true then -- a function that checks to see if a player is an admin
            local command,args = getCommand(msg) -- a function that converts a message to a command, and returns a table full of the arguments following the command in the message
            if command then -- if the message was a command
                cmds[command](args) -- fires the function
            end
        end
    end)
end)

This allows things to work easily when creating new commands. You will only need to put it in the cmds table, and the code associated with it.

The getCommand function returns:

  • A string (the name of the command) or nil if the command is not valid, or a prefix is not used
  • A table full of information used for the commands

Hope I helped!

~TDP

0
So theres no way just to use tables...? EnchantedGamerOne 0 — 8y
0
Yes, you can. My way is just better optimized for usage. I think I may have helped too much here o_o TheDeadlyPanther 2460 — 8y
0
oh? I just find it more organized with {"EnchantedGamerOne"} Instead of {["EnchantedGamerOne"] = true} EnchantedGamerOne 0 — 8y
0
Believe it or not either way you are iterating, so whether you use a dictionary or not is irrelevant. RemasteredBox 85 — 8y
View all comments (3 more)
0
@RemasteredBox No, you do not. A simple indexing is enough. With `local admins = { ["Player"] = true }`, you can directly know if a player is an admin like so: `admins["Player"]`. This will either return the associated value ('true'), or 'nil' -- which is falsey -- if there is no key-value pair with that key; whereas with the other method, you *have* to traverse the whole table and compare Link150 1355 — 8y
0
each element with the player's name to know if he is part of the table. Link150 1355 — 8y
0
Err.. why are people dissing my answer? I don't see anything wrong.. This is how you make admin commands work the best. Read what Link150 said about dictionaries. TheDeadlyPanther 2460 — 8y
Ad

Answer this question