When player chats a specific message, player's head makes a sound

Trying to make it so when a player chats a specific message, the players head makes a noise to act as if the player is saying something. I tried it two different ways but for some reason the player’s head isn’t being detected and the sound doesn’t play. An easy fix in someone’s eye but since I mainly code UI, not really sure what I am doing wrong.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if message == "Hello" then
			local playerHead = player.Character:FindFirstChild("Head")
			local newSound = Instance.new("Sound", playerHead)
			newSound.SoundId = "rbxassetid://12073820860"
			newSound.Volume = 1
			newSound:Play()
			newSound.Ended:Connect(function()
				newSound:Destroy()
			end)
		end
	end)
end)
local Text = "Hello"
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if Message:lower():match(Text:lower()) then
			local newSound = Instance.new("Sound")
			newSound.SoundId = "rbxassetid://12073820860"
			newSound.Volume = 1
			newSound.Parent = Player.Character.Head
			newSound:Play()
			game:GetService("Debris"):AddItem(newSound, newSound.TimeLength + 0.1)
		end
	end)
end)

Don’t use the second parameter for Instance.new()

I think you are only running this function when the player is added. Don’t you need to run the a separate function to detect if the player is chatting (I may be completely wrong here).

Also, load the sound into the head when the player is added so it’s stored there, not inside the function every time the player chats. Just use the “hello” chat to play the sound that’s already loaded in the head.

Code you provided doesn’t work.

What doesn’t work?

The whole script. Chatted “Hello” and nothing happened.

local Text = "Hello"
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local newSound = Instance.new("Sound")
		newSound.SoundId = "rbxassetid://12073820860"
		newSound.Volume = 1
		newSound.Parent = Player.Character:WaitForChild("Head")
	end)
	Player.Chatted:Connect(function(Message)
		if Message:lower():match(Text:lower()) then
			Player.Character.Head.Sound:Play()
		end
	end)
end)

try this.

Tried that and still nothing happened.

Is the sound being created at all?

Well originally, we had it to where if a Player 1 said something, a random player (Player 2 out of the player list) would play the audio to act as a reaction type thing to what Player 1 said. Now our player base for our game wants it so the Player 1 chats a specific message and their head makes a sound. Since the person who originally worked with us for this script is no longer around, I’m kinda just winging it.

Old code is below if that helps:

local function chatEvent(player, soundId, volume)
	local allPlayers = Players:GetPlayers()
	local chosenHead = nil

	while not chosenHead and #allPlayers > 0 do
		local chosenIndex = math.random(#allPlayers)
		local playerToCheck = allPlayers[chosenIndex]

		if playerToCheck.Character then
			chosenHead = playerToCheck.Character:FindFirstChild("Head")
			print(playerToCheck.Name .. " was chosen for a chat event")
		end

		table.remove(allPlayers, chosenIndex)
	end

	if chosenHead then
		local newSound = Instance.new("Sound", chosenHead)
		newSound.SoundId = "rbxassetid://"..tostring(soundId)
		newSound.Volume = volume
		newSound:Play()
		newSound.Ended:Connect(function()
			newSound:Destroy()
		end)
	end
end



Players.PlayerAdded:Connect(function(player)
	muffinPlayers[player.UserId] = false
	burritoPlayers[player.UserId] = false
	chocolatePlayers[player.UserId] = false
	balloonPlayers[player.UserId] = false
	sleepPlayers[player.UserId] = false
	urDonePlayers[player.UserId] = false
	
	chattedEvents[player.UserId] = player.Chatted:Connect(function(msg)
		if msg == "The muffin man?" then
			if not muffinPlayers[player.UserId] then
				muffinPlayers[player.UserId] = true
				
				chatEvent(player, 12073820860, 1)
				
				spawn(function()
					wait(60)
					
					muffinPlayers[player.UserId] = false
				end)
			end
		elseif msg == "Who wants a burrito?" then
			if not burritoPlayers[player.UserId] then
				burritoPlayers[player.UserId] = true
				
				chatEvent(player, 1131015006, 1.5)
				
				spawn(function()
					wait(60)
					
					burritoPlayers[player.UserId] = false
				end)
			end
		elseif msg == "Chocolate" then
			if not chocolatePlayers[player.UserId] then
				chocolatePlayers[player.UserId] = true
				
				chatEvent(player, 12385692922, 1)
				
				spawn(function()
					wait(60)
					
					chocolatePlayers[player.UserId] = false
				end)
			end
		elseif msg == "What is that in the sky?" then
			if not balloonPlayers[player.UserId] then
				balloonPlayers[player.UserId] = true

				chatEvent(player, 12434715719, 1)

				spawn(function()
					wait(60)

					balloonPlayers[player.UserId] = false
				end)
			end
		elseif msg == "Go to sleep" then
			if not sleepPlayers[player.UserId] then
				sleepPlayers[player.UserId] = true

				chatEvent(player, 1356600472, 1)

				spawn(function()
					wait(60)

					sleepPlayers[player.UserId] = false
				end)
			end
		elseif msg == "ur done" then
			if not urDonePlayers[player.UserId] then
				urDonePlayers[player.UserId] = true

				chatEvent(player, 12614040280, 1)

				spawn(function()
					wait(60)

					urDonePlayers[player.UserId] = false
				end)
			end
		end
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	chattedEvents[player.UserId]:Disconnect()
end)

No, it is not being created. Either that or the chat isn’t being detected. I’m not sure cause nothing is in the output.

It may be as simple as loading the sound into the head when the player joins as I suggested. It may take a split second for the sound to load every and every time you want to play it, and by that time it’s played and destroyed.
Try putting the following for troubleshooting purposes so you can see the time difference between the ‘loaded’, played and destroyed times.

	if chosenHead then
        print playerToCheck.Name  --I'm not sure of the hierarchy here, may need to modify this
		local newSound = Instance.new("Sound", chosenHead)
		newSound.SoundId = "rbxassetid://"..tostring(soundId)
        print("sound should be loaded")
		newSound.Volume = volume
		newSound:Play()
        print("sound being played")
		newSound.Ended:Connect(function()
			newSound:Destroy()
		end)
	end

The script is fine, it’s just that the audio is private or deleted, try choosing a different one

1 Like