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

Why is my 'Admin Commands' Chat function not executing the command?

Asked by 9 years ago

Yes, I am developing another Admin Commands system, but only with a better, and cleaner type of code [Different from my other ones, where they have allot of code], however, something is not executing the command, for example, when I attempt to say in the Studio, :kill player, it will not execute the command for some reason, but, when I type in reset, it will fire that command. Sadly, I am very confused on why it is doing this, and why it is not executing the command, I am thinking the error is at line 46, but I am not too sure, as that is supposed to prevent someone who is not an Admin from using the :kill player command. Here is the code I am currently using;

local Admins = {
['TheeDeathCaster'] = true;
['Player'] = true;
['yournamehere'] = true;
[44391621] = true;
}

local Banland = {
['someoneyouhate'] = true;
[40265333] = true;
}

local Prefixes = {":",";","/","_"}

function ChkPrefix(str)
local Prefix = "";
for i = 1, #Prefixes do
for x = 1, #str do
if str:sub(i,i) == Prefixes[i] then
Prefix = str:sub(i,i)
break
end
end
end
if Prefix ~= "" then
return Prefix
else
return false
end
end

function GetPlr(str)
local plrz = {};
for i,v in pairs(game.Players:GetPlayers()) do
if str ~= "" and (v.Name:lower():find(str) == (1)) then
table.insert(plrz,v)
end
end
return plrz
end

function Chats(plr,msg)

if (msg:lower():find("reset") == (1)) then return false, plr:LoadCharacter() end

if not Admins[plr.Name] and not Admins[plr.userId] and not ChkPrefix(msg) then return false end --Line 46, where I think the error is, although, this is where it is supposed to prevent a 'Non-Admin' to use the ':kill player' command
local Len = ChkPrefix(msg); msg = msg:sub(Len:len()+1) --Gets the length of the 'msg' of where the 'Prefix' is, then allows you to execute the command below;

if msg:lower():find("kill ") then
local chk1 = msg:lower():find("kill ");
local plrz = GetPlr(chk1+1);
for i,v in pairs(plrz) do
if v.Character then
v.Character:BreakJoints()
end
end
end

end

function AdminControl(plr)
if Admins[plr.Name] or Admins[plr.userId] then print(plr.Name," is an Admin!") end
if Banland[plr.Name] or Banland[plr.userId] then plr:Destroy() print(plr.Name," is currently banned.") end
plr.Chatted:connect(function(msg) Chats(plr,msg) end)
end

game.Players.PlayerAdded:connect(AdminControl)
for i,v in pairs(game.Players:GetPlayers()) do AdminControl(v) end

If you need me to tab my code correctly/better, I'll do it as soon as possible.

1
You really should tab your code correctly at the time you're writing it. It makes everything easier to read and debug. My dad was deducted points for not tabbing his code correctly when he was in collage. Just get into the habit. Perci1 4988 — 9y
0
If I may ask, because I think I got mixed up with another definition of 'tabbing' my code, because I keep thinking it's to seperate the different codes, or, to spread it out, may I ask what the correct definition for 'tabbing code' is? TheeDeathCaster 2368 — 9y
2
It's basically indenting using the 'tab' key. Pretty much every time you start a new scope, everything inside that scope should be indented one tab space to the right. http://wiki.roblox.com/index.php?title=Writing_Clean_Code#Indenting Perci1 4988 — 9y
0
Ah, that explains it, thanks for the explanation! :D However, tabbing like that gives me a harder time understanding a certain chunk of code for some reason, and I not am too sure why that is. TheeDeathCaster 2368 — 9y
0
This admin sucks. >.< TheeDeathCaster 2368 — 7y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

ChkPrefix is suspicious. You don't use x at all. Given the meaning of prefix, I would get you would want something like this:

function CheckPrefix(str)
    for _, prefix in pairs(Prefixes) do
        if str:sub(1, #prefix) == prefix then
            return prefix
        end
    end
end

For the chunk later that uses it, I think the following would be better:

    local pre = CheckPrefix(msg);
    if (not Admins[plr.Name] and not Admins[plr.userId]) or not pre then
        -- or instead of and
        -- `pre` instead of typing out CheckPrefix twice
        return false
    end 
    msg = msg:sub(#pre + 1)

msg is always used as msg:lower() so maybe just do that at the beginning.


GetPlr isn't given a string in the kill command. It's given a number (chk1 + 1) instead.

0
So, my code was working, but, I did allot of typos and such, which caused my code not to work properly? TheeDeathCaster 2368 — 9y
0
Not sure. These are at least two serious problems with the code -- you should do more debugging to find out if there's more -- and maybe while you're at it -- SLOW DOWN and DO UNIT TESTS (test all pieces) BlueTaslem 18071 — 9y
0
I have just tested the 'kill player' command, with a 'print' but, however, sadly, the 'GetPlr' function is not firing as supposed to, and I can not figure out why, because, if I attempted to print it, it would come out something like 'table: 2F24C958'. TheeDeathCaster 2368 — 9y
0
Try printing the members of the table then. A simple way to do print everything in a list if `print(unpack(list))`. BlueTaslem 18071 — 9y
View all comments (4 more)
0
Alright, I added a 'print' for that [print(unpack(plrz),"players.")], and this came up for some reason: nil players. TheeDeathCaster 2368 — 9y
0
Because of the nature of unpack you shouldn't follow it with anything. But that would just mean that `plrz` is empty. Learn to debug your own code! BlueTaslem 18071 — 9y
0
I think I'm just going to give up now, because; 1. I got my hopes too high up, and now I'm upset with the result of my code. 2. I have wasted allot/ too much of your time trying to answer my answer. 3. As it stands now, the code will not work, so matter how many time I try to recheck every chunk. Sorry for the inconvenience, and again, sorry for wasting your time trying to answer my question. TheeDeathCaster 2368 — 9y
0
I know this question & answer is old, but line 50 was the problem: I tried to get 'kill ' after it passed through, and so on line 51 it return the position of the string + 1, which would be 'ill [player]' instead of the original and intended purpose. TheeDeathCaster 2368 — 7y
Ad

Answer this question