How to check how many words a player chats

so I already have

game.Players.LocalPlayer.Chatted:Connect(function(msg)
		local split = string.split(msg, " ")
	end)

I wanted to know if I could see how many times it has to split or how many words there are.

You can use the “#” operator before a table to get the size of it.

game.Players.LocalPlayer.Chatted:Connect(function(msg)
		local split = string.split(msg, " ")
		print(#split)
	end)
local String = "The quick brown fox jumps over the lazy dog"
local Time = os.clock()

for _ = 1, 10000 do
	local Table = string.split(String, " ")
end

print(os.clock() - Time) --0.007804699998814613
local String = "The quick brown fox jumps over the lazy dog"
local Time = os.clock()

for _ = 1, 10000 do
	local _, Substitutions = string.gsub(String, " ", "")
	Substitutions += 1
end

print(os.clock() - Time) --0.003660899994429201

No surprise that string.gsub is more performant.

1 Like