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

How do I create arguments in an admin command?

Asked by 7 years ago

So, I want to create an admin command with two arguments. I'll be as follows: /add <player> <value> FYI, this is to add some points in the game. I know how to use string.sub(), but I'm not sure how to use it in this case. I'd appreciate any help given. Thanks.

1 answer

Log in to vote
0
Answered by 7 years ago

Let me begin by saying string.sub() isn't the only thing used to accomplish this.

string.find() is crucial to accomplishing what you want done.

Here's your script:

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        if string.sub(msg, 1, 5) == "/add " then -- Notice that extra space after 'add' inside of the quotations. That's to ensure that they're providing extra arguments.
            local playerMessage = string.sub(msg, 6, string.find(msg, " ", 6) - 1) -- This seems complicated although it's not. So, we want the first argument provided in the message chatted. What we're doing is using string.find() to find the next space (' ') after the 6th character in the full string typed. Since we know the first 1 - 5 characters are '/add ' we know that the sixth character will be the beginning of your first argument.
            local valueMessage = string.sub(msg, string.find(msg, " ", 6) + 1) -- Here we're doing everything as in the line above. Except, we're adding one instead of subtracting one because we want the value right after the third space in the string.

            if playerMessage == nil or valueMessage == nil then
                error("Argument(s) missing or not provided.")
            elseif playerMessage ~= nil and valueMessage ~= nil then
                local player = game:GetService("Players"):FindFirstChild(playerMessage)
                if player == nil then
                    error("The player provided doesn't exist!")
                else
                    print("Script ran fine! Your player's name is: " .. player.Name .. " and the value provided is: " .. valueMessage .. "!")
                end

            end
        end
    end)
end)
-- Note: This is a tested script, and it works. This is not 100% efficient, and can definitely be made more efficient to work with lots more commands and to make the scripting much easier.

I feel like I didn't fully provide enough explanation as to what string.find() does above. So, here's an in depth explanation:

First off, string.find() takes 4 arguments. Here's an example:

string.find("Hello world!", "!", 1, false)

The first argument is the string it will be using. The second argument is what it's searching for. The third argument is where it should start searching. The fourth argument isn't commonly used, although is defined as 'plain' so it does a substring search. I recommend just not including it.

The code above would actually return two values. It would return 12 and 12. So you could do:

local value1, value2 = string.find("Hello world!", "!", 1, false)
print(value1, value2) -- Prints '12 12' because the '!' is located at the 12th character of the string.

In the commands example I subtracted 1 and added 1 from the value returned by string.find(). I did this in order to get the value before the space, and the value after the space.

I hope I helped! :)

0
`find` essentially returns the position of the character(s) in a string found, but if it doesn't find a value, to note, it will return nil. TheeDeathCaster 2368 — 7y
Ad

Answer this question