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

When a player touches a part the audio plays but only for that client?

I already get how to do the script it's just I don't get how to make it only for a client.. when a player touches the the part a sound appears instead of getting it viral it only shows for that cilent.

1 answer

✓ Accepted answer

This is in terms of FE, I suggest you use RemoteEvents to gain your desired end-game. ROBLOX has pushed FE as a requirement as of recently, therefore I highly suggest you read up on this as it is quite useful, yet should be something familiar with.

For the following Code below, please make sure to have inserted a RemoteEvent in ReplicatedStorage

--// ServerScript - Child of Part.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

local Part = workspace.Part
local Sound = workspace.Sound

Part.Touched:Connect(function(PartTouched)
	if (PartTouched.Parent:FindFirstChildOfClass("Humanoid")) then
		RemoteEvent:FireAllClients(Sound) --// Sends a signal to everyone
	end
end)

--// LocalScript - Child of StarterPlayerScripts
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

RemoteEvent.OnClientEvent:Connect(function(Sound) --// Receives the signal
	Sound:Play()
end)

Though there is a simpler way with the Laws of FE. Since the Server broadcasts to all Clients, have the Server play the Sound.

--// ServerScript - Child of Part.

local Part = workspace.Part
local Sound = workspace.Sound

Part.Touched:Connect(function(PartTouched)
	if (PartTouched.Parent:FindFirstChildOfClass("Humanoid")) then
		Sound:Play()
	end
end)

Yes, this is simpler, though I suggest the use of example one. As it can be applied in other scenarios this method cannot. It's also best to stretch out your idea of something new before taking the easier route.

Hope this helps! Remember to accept and upvote this answer if so! Have a nice day

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