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..
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..
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:
Hope I helped!
~TDP
This discussion is preserved as it was. New answers, comments, and votes are closed.
7 comments
So theres no way just to use tables...?
Yes, you can. My way is just better optimized for usage. I think I may have helped too much here o_o
oh? I just find it more organized with {"EnchantedGamerOne"} Instead of {["EnchantedGamerOne"] = true}
Believe it or not either way you are iterating, so whether you use a dictionary or not is irrelevant.
@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 compareeach element with the player's name to know if he is part of the table.
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.