Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make this audio event play?

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

4 comments

0

nvm I figured it out

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

0

So I've gotten this far but now it's giving me a weird error

0

NVM NOOB mistake

1 answer

✓ Accepted answer

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

10 comments

0

What is Hit? That is no event. Stop using it. It just causes errors.

0

It doesn't even matter? lol

0

so @OldPalHappy then what should I change with his answer because I don't completely understand anonymous functions yet.

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

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.

0

Also made a woopsy, it isn't hit, it's Touched.

0

Omg thank you that clears up so much

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

0

You did game.Workspace.Players, but it is game.Players

0

And the Lua icon for the code block.

This discussion is preserved as it was. New answers, comments, and votes are closed.