I’m kinda new to scripting and I’ve made a script where when you touch a part a sound plays. However, I’ve noticed that the sound plays for everyone when I just want the sound to play for the player that touched the part.
Here’s the script I have:
local sound = script.Parent.Sound
local play = false
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and play == false then
play = true
sound:Play()
wait(1)
play = false
sound:Stop()
end
end)
Any help would be greatly appreciated on how I can accomplish this!
You would have to play the sound locally (so run the sound script in a local script). If you need the rest to be in a server script then you could use a remote event to fire from the server to the local side to have a local script play locally.
You would have an object called a “RemoteEvent” in ReplcatedStorage, then have the script say this:
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and play == false then
play = true
--this is where im changing the script
game.ReplicatedStorage.Event:FireClient(hit.Parent.Parent.Name)
--this is firing it only for one client, the player who touched it
wait(1)
play = false
end
end)
And then in a localscript, have this:
game.ReplicatedStorage.OnClientEvent:Connect(function()
--im going to assume theres a var called sound
sound:Play()
wait(1)
sound:Stop()
end)