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

Admin script still prints the error message for everyone if they don't use the prefix?

Asked by
awfulszn 394 Moderation Voter
7 years ago
Edited 7 years ago

Okay, so I am making an admin script and have run into a problem;

It prints the error message if a player tried to use a command without even using the prefix. And even does it if they are admin. But it does still do it correct if they use the prefix.

Here is my code;

local Admins = {["mosski123"] = true, ["Player1"] = true}
local prefix = ';' 

function onChatted(message, player)
    if message == prefix.."reset" and Admins[player.Name] then
        player.Character:BreakJoints()
else
    print("[LINC] "..player.Name.. " tried to use the 'reset command!'")
end
    if message == prefix.."rejoin" and Admins[player.Name] then
        game:GetService("TeleportService"):Teleport(game.PlaceId, player)
        if player.Character ~= nil then
        player.Character:remove()
        end
    end

end

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(message) onChatted(message, player) end)
end)

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

There is a small syntax error:-

-- you missed the  .. after player.Name
print("[LINC] "..player.Name " tried to use the 'reset command!'")

The second possible problem is the way you have setup the logic so:-

local Admins = {["mosski123"] = true, ["Player1"] = true}
local prefix = ';' 

function onChatted(message, player)
    -- we run this if statment
    if message == prefix.."reset" and Admins[player.Name] then
        -- code
    else
       -- code
    end

    -- we also run this statment even if we have ran a command
    if message == prefix.."rejoin" and Admins[player.Name] then
        -- code
    end
end

-- we should use elseif as only one of the ifs will run as only one command will do one thing
if [condition] then
    -- code
elseif [second condition] then
    -- code
end

There are some other things that should be changes in your code, firstly I would recommend that you use UserId instead of the players name as the id will not change. Secondly you do a lot of processing that can be done once ie, prefix.."reset" and Admins[player.Name].

Example:-

-- we should use the player id not their name
local Admins = {["1540993"] = true, ["0"] = true} -- my id ect
local prefix = ';' 

-- just an idea to remove the long if statements, you can still use your method but you would need to use elseif
local commandTable = {}

function commandTable.rejoin(message, player)
    player.Character:BreakJoints()
end


game.Players.PlayerAdded:connect(function(player)
    if Admins[tostring(player.UserId)] then -- we only connect the admin command chatted event for admins instead of checking this each time
        player.Chatted:connect(function(message) 
            if message:sub(1,1) == prefix then -- we check for a prefix before checking for commands

                -- we remove the prefix its not needed
                message = message:sub(2, #message)

                if commandTable[message] then -- we have a valid command
                    commandTable[message](message, player) -- we run the function in the table passing it the same data as in your code.
                end
            end
        end)
    end
end)

I hope this helps, please comment if you do not understand how / why this code works.

0
I will implement this into my game now, and if it works, I'll upvote and accept, if not I'll let you know. awfulszn 394 — 7y
0
I'm sorta confused on how to implement it. awfulszn 394 — 7y
0
Which part? User#5423 17 — 7y
Ad

Answer this question