How to detect if a player said something in chat?

Hello! I am making a system in my game where you can say “/e cry” or any other animations I have and it will load the animation on the player, so I made a server script and added this code:

game.Players.PlayerAdded:connect(function(plr)
	plr.Chatted:connect(function(msg)
		print("Player chatted!")
	end)
end)

It will not print if the player chats, does this need to be client sided?

Thanks for reading!

1 Like

Have you tested this in the roblox client? Chat in roblox studio is kinda buggy currently.
Also make sure nothing yields the Player.PlayerAdded for too long or the player will join before the function is hooked.
If you do have something yielding, you can just loop through the players currently ingame and make the Chatted a function.

1 Like

I tried doing it in the client, and it did not work. What do you mean by yielding, Like a loop that occurs in Player.PlayerAdded?

It is your preference if you want to do it on the client or not.
If you do it on the client do
game:GetService(“Players”).LocalPlayer.Chatted:Connect(function()

By yielding I mean, loops, :WaitForChild() etc.

3 Likes

I will try doing it on the client with what you said, thank you.

I’m not sure if the animations will replicate if you play them on the client. I don’t really use animations. Try experimenting with it.

1 Like

It worked on the client, thank you! I just need to fire a remote event to server and then simply load the animation on the humanoid.

If all you’re doing is playing a corresponding animation for the emote command, you don’t need client-to-server communication in order to play the animation. Load the animation onto the Animator, the animations will automatically replicate to other clients. You don’t want input lag on your client. Always do your visuals on the client.

3 Likes

Oh okay, I thought it would make it only appear for the client, thank you!

I am trying to do this, but it does not work. There are no errors:

game:GetService("Players").LocalPlayer.Chatted:Connect(function(msg)
	if msg:lower() == "/e sit2" then
		local humanoid = game.Players.LocalPlayer.Character.Humanoid
		local animator = humanoid:FindFirstChildOfClass("Animator")
		
		local track = animator:LoadAnimation(sit)
		track:Play()
	end
end)

You need to call animator:LoadAnimation() on an Animation

game:GetService("Players").LocalPlayer.Chatted:Connect(function(msg)
	if msg:lower() == "/e sit2" then
		local humanoid = game.Players.LocalPlayer.Character.Humanoid
		local animator = humanoid:FindFirstChildOfClass("Animator")
		
		local Animation = Instance.new("Animation")
		Animation.AnimationId = "animationid here"
		local track = animator:LoadAnimation(Animation)
		track:Play()
	end
end)

Not sure if you were calling it on an animation already or not.
If you were already calling it on an animation, the id could be wrong.

1 Like

So I put it in like this:

game:GetService("Players").LocalPlayer.Chatted:Connect(function(msg)
	if msg:lower() == "/e sit2" then
		local humanoid = game.Players.LocalPlayer.Character.Humanoid
		local animator = humanoid:FindFirstChildOfClass("Animator")

		local Animation = Instance.new("Animation")
		Animation.AnimationId = 6384257999
		local track = animator:LoadAnimation(Animation)
		track:Play()
	end
end)

And it gives an error saying that it has an invalid animation ID.

On phone. (Gone to sleep)

Animation id should be “rbxassetid://6384257999”

Ohh, thank you I will try it right now.

OH MY GOD THANK YOU SO MUCH BRO NOT ONE PERSON SAID THIS IN THE WORLD :heart: