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

Any ideas or answers for using multiple 'string.sub' situations?

Asked by 8 years ago

I am working on an admin, and I've always had issues with 'speed plr [n]'. What I am getting at is how do you add multiple 'subs' for example, you cant do...

if msg:lower():sub(1,2) == "ws" then
local plr = msg:sub(3)
local speed = 'what'?
end

Where it says 'what?' the player name is going to be different everytime so theres no exact way of doing this that I know of

Can someone give me a hand here and help me out please?

Thanks!

0
I've completed my answer. TheDeadlyPanther 2460 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

What you should be using is string.match (or string.find). You would also want to use string patterns, to allow a different value to be returned.

-- Format: string.match(stringToUse,stringToFind)

string.match("Hello","Hell") -- returns 'Hell'
string.match("Yes","No") -- returns nil

Here is some complicated code that essentially forms the backbone of any admin script:

local prefix = ":"
local valType = {}
valType.player = "player"
valType.number = "number"

local commands = {
    speed = {
        values = {valType.player,valType.number}, -- the values the command requires
        func = function(...) -- the function that does the command
            local args = {...} -- sets the arguments as a table
            local player = args[1] -- gets the player
            if player and player:IsA("Player") then -- if it really is a player
                local character = player.Character or player.CharacterAdded:wait()
                local humanoid = character:WaitForChild("Humanoid")
                args[2] = tonumber(args[2]) -- makes sure the second argument is a number
                if args[2] then -- if is a number
                    humanoid.Walkspeed = args[2] -- sets the walkspeed
                end
            end
        end
    }
}

function getArgs(msg)
    local args = {} -- table for arguments
    while msg ~= "" or string.match(msg," ") do -- repeats until all arguments have been captured
        local str = string.match(msg,"(%.)%n+") or msg -- [3]
        table.insert(args,str) -- adds the argument into the table
        local strEnd = string.match(msg,"(%n+)") or "" -- finds the whitespace character at the end of the match
        msg = string.gsub(msg,str..strEnd,"") -- removes the argument and the whitespace character
    end
    return args -- returns the arguments
end

function decodeMessage(msg)
    local pref = string.sub(msg,1,string.len(prefix)) -- [1]
    if pref == prefix then -- if the player used the correct prefix
        msg = string.gsub(msg,prefix,"") -- [2]
        local args = getArgs(msg) -- gets all of the arguments for individual use
        local cmd = cmds[args1] -- gets the command requested by the player
        if cmd then -- if it is an existing command
            table.remove(args,1) -- removes the command entry in the args
            args = assignValues(args,cmd.values) -- [4]
            cmd.func(unpack(args)) -- executes the function
        end
    end
end

[1]

This line finds the prefix, using the length of the prefix as a guide - string.match may be unreliable because someone may use ':' later in the script.

[2]

string.gsub essentially replaces one part of a string with another string. This returns the new string, so it does not automatically update variables.

-- Format: string.gsub(stringToUse,stringToFind,stringToReplaceWith)

string.gsub("Yes","Yes","No") -- returns "No"

[3]

String patterns:

() = Captures a pattern, returning what is captured. Multiple captures in one string returns a tuple.

%n = Whitespace character.

%. = Any character.

+ (suffix) = Gets as many characters as possible.

[4]

I won't write this function, but it essentially assigns each argument the corresponding argument defined in the 'values' array in a command's dictionary.

For example, for the speed command, it makes the first argument a player, and the second argument a number. I'm using 'valType' cause it looks cool (lol), and just organises everything better.


I understand this is very very very very very very complicated for beginners, so this will probably look very daunting (especially in the SH code block).

If you need some more help understanding parts of it, please ask.

Also note that this code is theoretical, so it may not work (I probably have made some typos, too)


Hope I helped!

~TDP

0
And this is supposed to set the player's value to value, eg: Speed PLAYER [ Third msg ] eg, :speed me 30 -- sets speed to 30 Expresssion 5 — 8y
Ad

Answer this question