--ok so I went by what kiiln said but it's saying that Players is not a valid member of workspace (line 5) Even though it is
local hasHit = false local scarepart = game.workspace.ScareBrick
scarepart.Touched:connect(function(partth) if partth.Parent:FindFirstChild("Humanoid") and game.workspace.Players[partth.Parent.Name] and not hasHit then local plr = partth.Parent local sound = scarepart.Sound:clone() sound.Parent = game.workspace.Players[plr.Name].PlayerGui sound:Play() hasHit = true end end)
Going to introduce you to anonymous functions
.
Now, to keep them from playing the sound too many times, you can use a debounce
.
Debounce makes it so they can't hit it more than once.
I'll also make it so if the players hits it, it will only play for THAT player, and not everyone in the server.
01 | local hasHit = false --They haven't hit the part yet |
02 | local soundBrick = game.workspace.ScareBrick --Where the Sound's parent is located |
03 |
04 | soundBrick.Touched:connect( function (partThatHit) --This is an anonymous function |
05 | if partThatHit.Parent:FindFirstChild( "Humanoid" ) and game.Players [ partThatHit.Parent.Name ] and not hasHit then --If the model has a Humanoid, they are in the Player list, and they haven't hit it yet |
06 | local plrCharacter = partThatHit.Parent |
07 | local sound = soundBrick.Sound:Clone() --Getting the sound |
08 | sound.Parent = game.Players [ plrCharacter.Name ] .PlayerGui --Putting the sound in the player's PlayerGui |
09 | sound:Play() --And play! |
10 | hasHit = true --Now they hit it! |
11 | end |
12 | end ) --After the function, it requires and end with a ) after it |