How to make a sound play every time a player says something

Hello everyone, how are you doing.
After a lot of failed attempts I decided to create a draft to ask for help.
How do I make a script where if a player says something, a random sound would play. And the amount of times a sound would play depends on the amount of words in a player’s sentence/message. This idea is similar to a game called:
generic roleplay gaem. - Roblox.
When you say something in that game, a mumbling noise would play as if the player was actually talking.

Any kind of help would be appreciated.

3 Likes

Here is an example on how to get the word count of a chat message:

game.Players.PlayerAdded:Connect(function(player)
  player.Chatted:Connect(function(msg)
        
    local wordCount = #msg:split(" ")

  end)
end)

This can come in really handy, but I’m afraid I don’t know how to make the script with the given help.

1 Like

I think that this could be a simple solution to your problem:

local sound = ? -- the sound clip object

game.Players.PlayerAdded:Connect(function(player) -- when a player joins
  
  player.CharacterAdded:Connect(function(char) -- when a player's character is loaded into the game

    local clone = sound:Clone()
    clone.Name = "NAME"

    clone.Parent = player.Character.Head

  end)

  player.Chatted:Connect(function(msg) -- when a player sends a message
        
    local wordCount = #msg:split(" ") -- get the count of words seperated by a SPACE
    if player.Character and player.Character.Head:FindFirstChild("NAME") then -- check if the cloned sound still exists

      local clone = player.Character.Head:FindFirstChild("NAME")
      clone:Play() -- play

      task.wait(wordCount) -- play the sound for the amount of words in message

      clone:Stop() -- stop the sound

    end

  end)
end)
1 Like
	plr.Chatted:connect(function(msg)
		local msgLength = msg:len();

		if plr.Character:FindFirstChild("Head") then
			for i = 0, msgLength do 
				local soundId = "7772738671";
				local soundInstance = Instance.new("Sound");
				soundInstance.Parent = plr.Character.Head;
				soundInstance.SoundId = "rbxassetid://"..soundId;
				soundInstance:Play();
				soundInstance.MaxDistance = 10;
				soundInstance.Ended:Wait();
				soundInstance:Destroy(); end;
		
		else return nil;
	end;
	end);
	
end);
3 Likes

Is this the type of thing you were looking for?

Both of them actually works so I’m not sure which one to mark it as solution.

when a player inputs more than one space rather than just one it doesnt work is there a better way than :split?