Voice Chat thing is not working properly

Hi developers. I am trying to make a system that senses when a player says a certain word and runs an animation and a small sound effect. For example, if a player says “hi” in chat their avatar does a wave animation and says “Hi!”.
My current script works just fine, but if you say “snindhghidjfkngfdngj” (there is a hi tucked in there if you cant see it) my script will run the animation and the sound. I know that this is because I am using string.match(), but how can I find the “hi” in the message without using string.match()?

Here is my script:

local hello = {"hello", "hi", "hoi", "sup", "hey", "henlo", "helo", "hola", "howdy"}


game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local can = true
		for _, text in pairs(hello) do
			if can then
				local contains = string.match(msg, text)
				if contains ~= nil then
					can = false
					local s = Instance.new("Sound", plr.Character.Head)
					s.SoundId = "rbxassetid://138632881"
					s:Play()
					local a = Instance.new("Animation")
					a.AnimationId = "rbxassetid://507770239"
					local la = plr.Character.Humanoid:LoadAnimation(a)
					la.Looped = false
					la:Play()
					wait(1.5)
					la:Stop()
					can = true
				end
			end
		end
	end)
end)
1 Like

If you want to run the animation and the sound on a specific string you can just directly compare it.

if message:lower() == "hi" then playTheMusicAndTheAnimation() end

Is there any errors, or any warnings in the output?

No…
I am wanting it to run the if you can find the word “hi” in their message. For example, if they say “Hi there!” I want it to run the animation.

Like I said in the post, it works just fine but I think I can make it better.

You could just make the variable for "hi" to " hi", maybe even " hi ", but another solution is just to put if msg == text. This will prevent people putting multiple in their message and causing it to play multiple times.

Well if thats what you want you can use coroutines.

What is the player says just “Hi”?
What you are suggesting simply is not good programming. Your suggestion does not allow for anything like punctuation. If you read the post, I am asking how to effectively use sting.find() to accomplish my goal.

Just do string.find(msg, text).

This will still run if the player says “ifdbgdhhi

Using string.find() is better because it will only play once. The reason your message is doing that is not because of what your using to find the string it’s because of you variable. It’s only looking for the hi. Can you show me how you want it to find the variable?

What do you mean

Also the current code with string.match() only plays once.

You can do this instead.
This is just an example:

local Players = game:GetService('Players')

local Array = {'hello', 'hi', 'howdy'}

Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		for _,v in ipairs(Array) do -- ipairs is used for arrays.
			if Message:lower() == v then
				print(v)
			end
		end
	end)
end)

Or, if you want to find the word itself in a message, you can use string.find.
Example here:

local Players = game:GetService('Players')

local Array = {'hello', 'hi', 'howdy'}

Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		for _,v in ipairs(Array) do -- ipairs is used for arrays.
			if Message:lower():find(v) then
				print(v)
			end
		end
	end)
end)

Will this print(v) when a player says any message in which the letters “H” and “I” are next to each other? I have been having that issue. When a player says “This” or “Him” the animation will play.

U can split your string into words and check if the word “hi” is found:

local message = "replace this with the meassge. Hi"
local words = message:lower():split(" ")
if table.find(words, "hi") then
  -- Hi is found
end

FYI: string.split and table.find is only implemented in Roblox-Lua and standard Lua doesn’t have those functions

2 Likes

I provided you with the code, here’s a guide on string manipulation: Click here.

I’m sure you can understand what’s going on here if you read that.

Thanks heaps! Took long enough…

Your solution is not what OP explained though.
I believe he wanted a list of words, which I clearly made a post about above.
https://prnt.sc/t5mjdh