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

How do i properly get arguments in an admin script?

Asked by 7 years ago

This is my code.

function oncommandReceivedEventFired(player, msg)
    local Temp = string.sub(msg, 2)
    Temp = string.lower(Temp)
    local Command = ""
    for i = 0, #Temp do
        if (string.byte(string.sub(Temp, i, i)) == 32) then
            Command = string.sub(Temp, 0, i - 1)
            Temp = string.sub(Temp, i + 1)
            break
        end
    end

    local arguments = {"dev"}
    local listpos = 1

    for x = 0, #Temp do
        if (string.byte(string.sub(Temp, x, x)) == 32) then
            local Arg = string.sub(Temp, 0, x-1)
            Temp = string.sub(Temp, 0,x)
            table.insert(arguments, listpos, Arg)
            listpos = listpos + 1
        end
    end

    --For debugging purposes. --

    for y = 0, #arguments do
        print(arguments[y])
    end
end

For some reason it's not working. Its supposed to detect spaces and cut the string and store it. I'm doing this because i want the ability to have an infinite amount of parameters on a command. It's not entirely finished. I'm stuck trying to fix this.

No errors nothing. Probably a miscalculation somewhere but i can't find it..

My commands look like this: '$this is a test'

The dollar sign was stripped from the string before it was sent to a normal script using a remote event.

Please help me

-ScrappyHaxor

1 answer

Log in to vote
0
Answered by 7 years ago

The easiest way I see doing it is using string.gmatch() and matching words then inserting them into a table.

local msg = "$this is a test"
local arguments = {}
for arg in msg:gmatch("%w+") do
    table.insert(arguments, arg);
end
print(arguments[1]) --> this
print(arguments[2]) --> is
Ad

Answer this question