Ad
Log in to vote
0

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

Asked by 1 year 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 — 1y

2 answers

Log in to vote
2
Answered by
uhi_o 417 Moderation Voter
1 year ago
Edited 1 year 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 2326 — 1y
Ad
Log in to vote
1
Answered by
TTChaos 750 Moderation Voter
1 year 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 TTChaos 750 — 1y

Answer this question