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

How to search for a space in chat?

Asked by
stepatron 103
6 years ago
Edited 6 years ago

This is for an admin script. How do I have it search for the second space? So like ``;;setHealth [Player] [newHealth]" I cant figure out how to have the script 'know' what part is the name and what part is the value that the health will be set to

1 answer

Log in to vote
1
Answered by
Avigant 2374 Moderation Voter Community Moderator
6 years ago

String patterns are just what you need. I recommend splitting the string after the signal character(s) (in your case, the ";;") into a command and argument.

local SignalCharacters = ";;"
local Message = ";;setHealth Username 100"

function HandleMessage(Message)
    if Message:sub(1, #SignalCharacters) ~= SignalCharacters then
        -- Return early if message isn't a command
        return
    end

    local CommandSubstring = Message:sub(#SignalCharacters + 1)

    local Command = nil
    local Arguments = {}

    for Match in CommandSubstring:gmatch("%w+") do
        -- If there's not a first match yet, it's the command!
        if  Command then
            table.insert(Arguments, Match)
        else
            -- If there's not a first match yet, it's the command!
            Command = Match
        end
    end

    if not Command then
        return
    end

    -- Continue here to check if it's a valid command, and has the valid number of arguments, et cetera.
end

HandleMessage(Message)
0
Im afraid I dont quite understand what everything here does stepatron 103 — 6y
0
I recommend you check out the Wiki article I linked so you'd understand a little more! %w is the word character class, which will match an alphanumeric character, and the + quantifier matches 1 or more of the previous specifier. Avigant 2374 — 6y
Ad

Answer this question