One good way to do this is to save the players who have touched the part and not play the sound if they're in the dictionary. like this. (btw I'm saving it by their user id):
local players = gam
local playersWhoTouched = {}
script.Parent.Touched:Connect(function(otherPart)
if part.Parent:IsA("Model") and players:GetPlayerFromCharacter(part.Parent) then
local plyr = players:GetPlayerFromCharacter(part.Parent)
if not playersWhoTouched[plyr.UserId] then
playersWhoTouched[plyr.UserId] = true
game.Workspace.THEPART.WinSound:Play()
end
end)
This will work but the problem is as players join touch the part and leave, its possible large numbers of user ids could accumulate in the dictionary, so for the sake of this situation we will remove them from the table when they leave the server like so:
local players = gam
local playersWhoTouched = {}
script.Parent.Touched:Connect(function(otherPart)
if part.Parent:IsA("Model") and players:GetPlayerFromCharacter(part.Parent) then
local plyr = players:GetPlayerFromCharacter(part.Parent)
if not playersWhoTouched[plyr.UserId] then
playersWhoTouched[plyr.UserId] = true
game.Workspace.THEPART.WinSound:Play()
end
end)
players.PlayerRemoving:Connect(function(plyr)
playersWhoTouched[plyr.UserId] = nil
end)
There you go. If you need further explanation, ask in the comments.
1 comment
is there any errors?