Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I make this audio event play?

Asked by 7 years ago
Edited 7 years ago

--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)

0
nvm I figured it out macabe55101 38 — 7y
0
So I fixed the last problem, but now I want it to only play every once in a while not just spaz out and repeat like 8 times as their walking through it macabe55101 38 — 7y
0
So I've gotten this far but now it's giving me a weird error macabe55101 38 — 7y
0
NVM NOOB mistake macabe55101 38 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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.

local hasHit = false --They haven't hit the part yet
local soundBrick = game.workspace.ScareBrick --Where the Sound's parent is located

soundBrick.Touched:connect(function(partThatHit) --This is an anonymous function
    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
        local plrCharacter = partThatHit.Parent
        local sound = soundBrick.Sound:Clone() --Getting the sound
        sound.Parent = game.Players[plrCharacter.Name].PlayerGui --Putting the sound in the player's PlayerGui
        sound:Play() --And play!
        hasHit = true --Now they hit it!
    end
end) --After the function, it requires and end with a ) after it

0
What is Hit? That is no event. Stop using it. It just causes errors. OldPalHappy 1477 — 7y
0
It doesn't even matter? lol BlockSoar 0 — 7y
0
so @OldPalHappy then what should I change with his answer because I don't completely understand anonymous functions yet. macabe55101 38 — 7y
1
I really appreciate youre response, but I dont understand how the (function(hit) works especially the part before that says "soundBrick.Hit:connect" but how does the script know where and what hit is??? macabe55101 38 — 7y
View all comments (6 more)
0
I changed the function(hit) so it will be a bit more clear. Go to the link for anonymous functions and read up on that. SimplyRekt 413 — 7y
0
Also made a woopsy, it isn't hit, it's Touched. SimplyRekt 413 — 7y
0
Omg thank you that clears up so much macabe55101 38 — 7y
0
ok so I got a version of what you did up and running but it fails at line 5 (see updated post above) and says Players isn't a valid member of workspace, btw how do I caopy my code neatly like you had macabe55101 38 — 7y
0
You did game.Workspace.Players, but it is game.Players SimplyRekt 413 — 7y
0
And the Lua icon for the code block. SimplyRekt 413 — 7y
Ad

Answer this question