Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make a sound effect play for only one player when they click a click detector?

I made a script that will display a series of text on screen when a person clicks a certain block. A sound effect is supposed to play every time the text switches to a new line, but the only way I've found to successfully do this is make it so that every player can hear the sound, which isn't what I want.

local cd = script.Parent.ClickDetector
local thoughtLines = {}

cd.MouseClick:Connect(function(plr)
	local playerGui = plr.PlayerGui
	local gui = playerGui.ScreenGui
	local textLabel = gui.TextLabel
	
	gui.Enabled = true
	thoughtLines = {"Sample1", "Sample2"}

	for i,v in pairs(thoughtLines) do

		-- Sound Effect executed
		plr.PlayerGui.notifSpeak:Play()

		textLabel.Text = (thoughtLines[i])
		wait(3)
	end

	gui.Enabled = false
end)

If it helps, this script is in a server script.

This script also didn't seem to work in a local script:

local cd = script.Parent.ClickDetector
local SoundService = game:GetService("SoundService")
local thoughtLines = {}
local notf = Instance.new("Sound")

local function playLocalSound(soundId)
	local sound = Instance.new("Sound")
	sound.SoundId = soundId
	SoundService:PlayLocalSound(sound)
	sound.Ended:Wait()
	sound:Destroy()
end

cd.MouseClick:Connect(function(plr)
	local playerGui = plr.PlayerGui
	local gui = playerGui.ScreenGui
	local textLabel = gui.TextLabel

	gui.Enabled = true
	thoughtLines = {"Sample1", "Sample2"}

	for i,v in pairs(thoughtLines) do
		playLocalSound("rbxassetid://4590662766")
		textLabel.Text = (thoughtLines[i])
		wait(3)
	end

	gui.Enabled = false
end)

1 comment

0

You can put it in a local script

1 answer

Like SuperPuiu said, you can put it in a local script. You can use the following script found in https://developer.roblox.com/en-us/api-reference/function/SoundService/PlayLocalSound

local SoundService = game:GetService("SoundService")
 
-- Create custom plugin button
local toolbar = plugin:CreateToolbar("Empty Script Adder")
local newScriptButton = toolbar:CreateButton("Add Script", "Create an empty Script", "rbxassetid://1507949215")
 
local function playLocalSound(soundId)
	-- Create a sound
	local sound = Instance.new("Sound")
	sound.SoundId = soundId
	-- Play the sound locally
	SoundService:PlayLocalSound(sound)
	-- Once the sound has finished, destroy it
	sound.Ended:Wait()
	sound:Destroy()
end
 
local function onNewScriptButtonClicked()
	-- Create new empty script
	local newScript = Instance.new("Script")
	newScript.Source = ""
	newScript.Parent = game:GetService("ServerScriptService")
	-- Call function to play local sound
	playLocalSound("")  -- Insert audio asset ID here
end
 
-- Connect plugin button to action function
newScriptButton.Click:Connect(onNewScriptButtonClicked)

1 comment

0

I tried doing this but it didn't end up working. 

This discussion is preserved as it was. New answers, comments, and votes are closed.