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

Strings - how do you detect the third word a player says?

Asked by 3 years ago

So I get that you can use string.sub to check if the player's first few letters are a prefix, and then you can use whatever they say after that as a player's name, or whatever.

But how exactly do you find out where the second word ends? I mean, let's say I wanted to make a command that would forcefield a player for a duration of time

:ff bluehawks 20

How would I actually get the third word? In this case it's a number, but what if it wasn't?

1
you could try doing (string:split(" "))[3] mybituploads 304 — 3y

2 answers

Log in to vote
2
Answered by
uhi_o 417 Moderation Voter
3 years ago
Edited 3 years ago

So you can use string.split method for strings that roblox has implemented.

Here's an example

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        local ThirdWord = msg:split(" ")[3] --Splitting the message which returns an array
        --[[ Here is just extra because I feel like it
        if typeof(ThirdWord) == "number" then --Checking if it's a number
            --Do your thing
        end
        ]]
    end)
end)

If you are more visual let me show you something here

local Message = ":ff uhi_o 40"
local Words = Message:split(" ") --Returns an array like so {":ff", "uhi_o", "40"}
local ThirdWord = Words[3] --Getting the third element 
0
woah, i learned something new today raid6n 2196 — 3y
Ad
Log in to vote
1
Answered by 3 years ago

Just split the strings.

local Message = ":ff bluehawks 20"

function Split(Command, Separator)
    Seperator = Seperator or "%s"
    Arguments = {}
    for String in Command:gmatch("([^"..Seperator.."]+)") do
        table.insert(Arguments, String)
    end
    return Arguments
end

local Duration = Split(Message)[2]

See this: https://stackoverflow.com/questions/1426954/split-string-in-lua

0
you know what nevermind User#30567 0 — 3y

Answer this question