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