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)
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
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.