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?
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:
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.
4 Likes