How I make chat sound play every time player chat

Hello,
I would like to play a sound that play every time player send a message in the game what function I should use to detect if the player are chatting and how I use them?

You can use a Player.Chatted event listener. When the event fires, you can play a Sound.

1 Like

Basically some simple code would look like this.

local Player = game:GetService("Players")
local player = Player.LocalPlayer
player.Chatted:Connect(function(message)
    if message == "Hello" then
    -- Run stuff here
    end
end)

I type this in the ServerScript and put the sound thing to the SoundService and I run this code but nothing are working can you check what is wrong with the code please?

    game.Players.PlayerAdded:Connect(function(player)
        	player.Chatted:Connect(function(msg)
        		local Sound = game.SoundService.Sound
        		if msg then
        			Sound:Play()
        		end
        	end)
        end)
1 Like

The solution for this is using RemoteEvent.

-- Server Script
local _playEvent = game:GetService("ReplicatedStorage"):WaitForChild("PlaySound")

game.Players.PlayerAdded:Connect(function(_player)
	_player.Chatted:Connect(function(_msg)
		_playEvent:FireAllClients()
	end)
end)

Here, we check if somebody type smth in chat via server script, if somebody does, we fire all clients, and they do this:

--Local Script
   local _playEvent = game:GetService("ReplicatedStorage"):WaitForChild("PlaySound")

   local _sound = game:GetService("SoundService"):WaitForChild("ErikaMinecraftParody") -- or whatever u want

   _playEvent.OnClientEvent:Connect(function()
	_sound:Play()
end)

Explorer panel if u don’t understand how to organize this all:

explorer

So now when server fired all clients and clients got this, they can play this sound.

Hope this solution helped u out, if u wanna more details, please message here.

6 Likes