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

Receive Arguments for admin script?

Asked by
Bman8765 270 Moderation Voter
9 years ago

Okay, so my admin script will have multiple commands. One of these commands is :sethealth which will allow you to set the health of another player. I already have 1 command in my admin script called :kill and it works just fine but what I just realized in I'm string string:sub to check to see what command they type, although this works with :kill when there are only 2 arguments what do I do when there are 3 arguments that could be any size (For example, :sethealth bman8765 100 but I also could do :sethealth Bman8765 20 which would be a different sized string so the msg:sub numbers would need to be changed). I was thinking about using gMatch to do this but idk if that is the best way to do it and if it is, how should I do it? If possible I wanna try to make a function that will return all the arguments in a table so I can easily access them.

Any advice helps! Thanks! If you have any questions just ask, I'm not always the best at explaining things.

local adminSettings = require(script["Bman Admin Settings"])
local adminStartCommand = adminSettings["startCommand"]

-----------------------------------------------------------------------------------------
--Functions that do the stuff
-----------------------------------------------------------------------------------------
function createArguments(message)
    local newtable = {}
    local value = 0
    for w in string.gmatch(message, "%a+") do
        value = value+1
        newtable[value] = (w)
    end
    return table
end

function adminCheck(name)
    for _, Name in pairs (adminSettings["Ranks"]["Admin"]) do
        if (Name == name) then
            return true
        end
    end
    --return false
end

adminCheck("Bman")

function findPlayer(name, adminWhoRanCMD)
    if (name:lower() == "me") then
        return adminWhoRanCMD
    end
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
        end
    end
end

function killPlayer(player) -- Kills the given player
    player.Character:BreakJoints()
end

function setHealth(player, amount)
    --player.Character.Humanoid.Health
end
-----------------------------------------------------------------------------------------
--Registering message
-----------------------------------------------------------------------------------------
function onChatted(message, player)
    if (message:sub(1,1) == adminStartCommand and adminCheck(player.Name)) then
        if (message:sub(2, 5) == "kill") then
            local victim = findPlayer(message:sub(7), player)
            killPlayer(victim)
        elseif (message:sub(2, 10) == "sethealth") then
            local victim = findPlayer(message:sub(11), player)
            local arguments = createArguments(message)
            --local amount = string.gmatch(message, "%a+")
            print(arguments[3])
        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
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

Using string.gmatch, you can find repeated patterns in a string.

In this case your pattern is " argument", note the space in front.

function onChatted(message, player)
    if (message:sub(1,1) == adminStartCommand and adminCheck(player.Name)) then
        if (message:sub(2, 5) == "kill") then
        for arg in string.gmatch(message, " (%w+)") do -- NOTE: this stores what is found by %w+ in arg, not the entire match.
            local victim = findPlayer(arg)
            killPlayer(victim)
        end
    end
end

When you need to consider arguments in relation to each other:

function onChatted(message, player)
    if (message:sub(1,1) == adminStartCommand and adminCheck(player.Name)) then
        if (message:sub(2, 7) == "dothis") then
        local args = {}
        for arg in string.gmatch(message, " (%w+)") do -- NOTE: this stores what is found by %w+ in arg, not the entire match.
            table.insert(args, arg)
        end

        print(args[1] .. ": " .. args[2])
    end
end

See Also:

String Patterns

Ad

Answer this question