Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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 comment

1

you could try doing (string:split(" "))[3]

2 answers

✓ Accepted answer

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 

1 comment

0

woah, i learned something new today

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

1 comment

0

you know what nevermind

This discussion is preserved as it was. New answers, comments, and votes are closed.