How do I make a random player's head play an audio when a player says a keyword in chat?

So I’ve been trying to script a funny little easter egg for my friend’s game.

I am trying to make it so whenever a player says a specific sentence/keyword within chat, it triggers a random player’s head in the game to play a sound. I tried doing this code, which I think is messy and probably wrong since I don’t really have much experience, but have no luck making it work.

Script is below:

local players = game:GetService("Players"):GetPlayers()
local randomPlayer = players[math.random(#players)]
local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")
local SoundReact = ReplicatedStorage:WaitForChild("PlayReact")

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg) 
		if msg == "The muffin man?" then
			SoundReact.OnServerEvent:Connect(function(plr, SoundId, Volume)
				if randomPlayer.Character then
					local HeadInstance = randomPlayer.Character:FindFirstChild("Head")
					if HeadInstance then
						local NewSound = Instance.new("Sound",HeadInstance)
						NewSound.SoundId = 12073820860
						NewSound.Volume = 1
						NewSound:Play()
						wait(1.5)
						NewSound:Destroy()
					end
				end
			end)
		end
	end)
end)

Here’s what I would do

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- stores all .Chatted events so they can be disconnected later
local chattedEvents = {}

Players.PlayerAdded:Connect(function(player)
	-- connect event and add it to the list
	chattedEvents[player] = player.Chatted:Connect(function(msg)
		-- early return, so we have less indentation (so it's easier to read)
		if msg ~= "The muffin man?" then
			return
		end

		local allPlayers = Players:GetPlayers()
		local chosenHead

		-- repeatedly randomly choose a player, until we find one that has a character or until there are no players left
		while not chosenHead and #allPlayers > 0 do
			local chosenIndex = math.random(#allPlayers)
			local playerToCheck = allPlayers[chosenIndex]

			if playerToCheck.Character then
				-- if they somehow don't have a head, chosenHead will stay nil
				chosenHead = playerToCheck.Character:FindFirstChild("Head")
			end

			-- remove from allPlayers, so we can't choose this player again
			table.remove(allPlayers, chosenIndex)
		end

		if chosenHead then
			local newSound = Instance.new("Sound", chosenHead)
			newSound.SoundId = 12073820860
			newSound.Volume = 1
			newSound:Play()
			newSound.Ended:Wait() -- wait until it has ended, however long it may be
			newSound:Destroy()
		end
	end)
end)

-- disconnect events
Players.PlayerRemoving:Connect(function(player)
	chattedEvents[player]:Disconnect()
	chattedEvents[player] = nil
end)

I don’t know what SoundReact is for so I ignored it.
And in your script you were making a list of all players and choosing one of them only once (when the game starts up (so with 0 players)) so I moved that. Other than that it’s pretty much the same.

It may have some small bugs because I wrote this here instead of in roblox studio, but I trust you can fix those :+1:

1 Like

It seems the sound wasn’t able to be found after the player has been chosen. So I tried re-routing the sound into ReplicatedStorage which seemed to allowed it to find the sound but the sound isn’t playing from the chosen player’s head still.

Also ran a check to ensure that the player is chosen/the trigger word was said which it confirmed. No relevant errors in the console though.

image

What do you mean? What error did you get?

Because it doesn’t look for a sound, it creates a new sound using Instance.new().

But now that you have the sound in ReplicatedStorage you can instead just :Clone() it.

Console output on the Server side shows:

No errors on client end.

However, after putting it in ReplicatedStorage and re-routing the code to such, error goes away but the sound is still not playing.

Is the id the same as the one in ReplicatedStorage?


Are you parenting it to anything in the workspace? If not you won’t be able to hear it.

Sound Id is actually a string, not a number. aka Sound.SoundId = 'rbxassetid://id' not Sound.SoundId = 123456789

1 Like

Yup, works as it should now with that change.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.