How to detect how many times a word appears in a text

okay so if i have this text: “1 1 1 1 1 2 2 2 2”

I want a script to fire a event everytime it finds a “1” in the text

any ideas on where to start?

I’m not the most experienced on string manipulation, but this document should help you start. I would guess that what you’re looking for is probably string.find. (You can read more about it in the document)

1 Like

Another method of doing this would be to use the string.split() method. For example:

local newMsg = msg:split(“ “)

for i,v in pairs(newMsg) do
    if v == 1 then
       — do something 
    end
end

Sorry about formatting

1 Like

You could try something like this:

for rep in str:gmatch("1") do
	-- do stuff
end
1 Like

You could use gmatch:

local count = 0
for match in string.gmatch("1 1 1 1 1 2 2 2 2", "1") do -- str:gmatch("1") is another option, as shown above
  count += 1-- This is specifically for counting, but you can just fire your event here as well.
end
print(count) -- 5
1 Like