Hello i am a beginner at coding and I’ve made this code that plays a sound when a player touches a part but i would like the sound to be played only for the player that touched the part what changes do i need to make to be able to achieve that.
local play = false
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and play == false then
play = true
sound:Play()
wait(7)
play = false
sound:Stop()
end
end)
play = false
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and play == false then
play = true
game.ReplicatedStorage.Event:FireClient(hit.Parent.Parent.Name)
wait(7)
play = false
end
end)
You can record the .Touched even of the part in a local script:
local Players = game:GetService("Players")
local Client = Players.LocalPlayer
local part = -Your part address
part.Touched:Connect(function(hit)
local model = hit.Parent
local character = Client.Character
if model and character and model == character then
sound:Play()
sound.Ended:Wait()
end
end)
You don’t need the RemoteEvent or server script to detect the part’s Collision.; you can rely on the client for that.
Also make sure that the sound is parented to the part.
local play = false
local sound = (your sound location here)
script.Parent.Touched:Connect(Function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
play = true
local s = sound:Clone
s.Parent = plr.PlayerGui
s:Play()
wait(7)
play = false
s:Stop()
s:Destroy()
end
end)