Need Help with a Soundbite system

So i’ve been using a super simple dialogue script (you stand next to an npc, you get a little gui with a typewriter printing system, yay) and I added a system that plays an extremely brief sound for each character that is typed, similar to the Animal Crossing gibberish or the Undertale voice system.
The main issue I have with the system is that although the GUI works properly, everyone in the server can hear the soundbite, from any distance - How would I make the sound only audible to the player talking to the NPC?
I’m not very good at coding, and this is the first game I’m making seriously, all help will be appreciated.
Here’s the script I’m using.

local debounce = false
local cooldown = 3



-- I also noticed that the "messages" seem to only be able to declared once - 
-- if I change their value within, say, the print loop, it doesn't print the new value.
-- Maybe I don't know what i'm doing? 
local Messages = {"hai", "welcome to the.. uh.. shop", 'mmmm... brownie', "aww man.. i forgot my lunch"}

local soundservice = game:GetService("SoundService")
local sound = soundservice.JakSound
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce == false then
			debounce = true

			local GUI = script.Parent.NpcDialogue:Clone()
			local Player = game.Players:GetPlayerFromCharacter(hit.Parent)

			local Rig = script.Parent.Parent:Clone()

			GUI.Parent = Player.PlayerGui
			GUI.Background.NpcName.Text = script.Parent.Parent.Name

			Rig.Parent = GUI.Background.ViewportFrame

			for i, v in pairs(Messages) do 
				sound:Play()
				for i = 1, string.len(v) do wait(.025)
					sound:Play()
					GUI.Background.DialogueText.Text = string.sub(v, 1, i)
					sound:Play()
				end
				sound:Play()
				wait(1)
			end

			wait(cooldown)
			debounce = false
			GUI:Destroy()
		end
	end
end)

Thank you!

1 Like

You can try put the sound inside the GUI so that the sound plays for that player with the GUI.

Trying this out rn, i’m gonna be psyched if it’s that simple

Didn’t work, I dropped the sound inside the GUI, fixed the path, and sound still played correctly but the other testers in the server could still hear it.

Did you used the roblox test thing where it opens up other windows for each player?

Could you also provide your updated code?

Yeah, I used the group test, here’s the code:

local debounce = false
local cooldown = 3
 
local Messages = {"test dialogue"}

local soundservice = game:GetService("SoundService")
local sound = Workspace.soundbitetest.Trigger.NpcDialogue.Sound

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce == false then
			debounce = true
			
			local GUI = script.Parent.NpcDialogue:Clone()
			local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
			
			local Rig = script.Parent.Parent:Clone()
			
			GUI.Parent = Player.PlayerGui
			GUI.Background.NpcName.Text = script.Parent.Parent.Name
			
			Rig.Parent = GUI.Background.ViewportFrame
			
			for i, v in pairs(Messages) do 
				sound:Play()
				for i = 1, string.len(v) do wait(.025)
					sound:Play()
					GUI.Background.DialogueText.Text = string.sub(v, 1, i)
					sound:Play()
				end
				sound:Play()
				wait(1)
			end
			
			wait(cooldown)
			debounce = false
			GUI:Destroy()
		end
	end
end)

I am not sure where it is getting the sound from, is it the same “NpcDialogue” as the line that says “local GUI = script.Parent.NpcDialogue:Clone()”

Oh, I parented the sound to the one that’s already under the trigger - Hold on, let me screenshot this:
image
Trigger is parented to an NPC named ‘soundbitetest’.

The code seems to be playing the sound not from the copy but from the trigger part which is why it is heard from other players. You need to make not do sound:Play() because it is playing the sound from the trigger’s GUI and not the player GUI.

How would I play the sound from the clone? I think I’m starting to get the theory of how this works, but i’m really new to Lua scripting, so I don’t have the practical knowledge to carry that out.

local debounce = false
local cooldown = 3

local Messages = {“test dialogue”}

local soundservice = game:GetService(“SoundService”)

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
if debounce == false then
debounce = true

		local GUI = script.Parent.NpcDialogue:Clone()
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		local Rig = script.Parent.Parent:Clone()
		
		GUI.Parent = Player.PlayerGui
		GUI.Background.NpcName.Text = script.Parent.Parent.Name
		
		Rig.Parent = GUI.Background.ViewportFrame
		
		for i, v in pairs(Messages) do 
			sound:Play()
			for i = 1, string.len(v) do wait(.025)
				GUI.Sound:Play()
				GUI.Background.DialogueText.Text = string.sub(v, 1, i)
				GUI.Sound:Play()  -- Gets Sound from GUI because the GUI is the clone being put into the player's GUI
			end
			GUI.Sound:Play()
			wait(1)
		end
		
		wait(cooldown)
		debounce = false
		GUI:Destroy()
	end
end

end)

To be honest I am not a really good coder as well, but I think this code will work. (Make sure to copy and paste it correctly because some text is for some reason not apart of the code.)

Trying this out in a bit, hopefully it works

I’m getting the following error:
Workspace.soundbitetest.Trigger.Trigger_ui:24: attempt to index nil with ‘Play’
The dialogue also breaks as a result:


Normally the Messages would print underneath ‘soundbitetest’.

I forgot to change the sound:Play() to GUI.Sound:Play() on that line. woops

Oh, lol, I should’ve seen that, trying again

After quite a while of playing with the limitations of the version, I’m happy to say your script fixed my problem completely! Thank you so much!

1 Like