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

How to make sound play when touched by a person?

How do I make sound play when a part is touched by a person and not NPC. I have walking npcs walking around and they keep messing up the sound. How would I make it so the sound would only play when a real player touches the part?


local sound = Instance.new("Sound", Workspace)
sound.Pitch = 1 
sound.SoundId = "rbxassetid://902587196" 
sound.Volume = 1 
sound.Looped = false 
local playonce = false

script.Parent.Touched:Connect(function()
    if not playonce then
        playonce = true
            sound:play()
    end
end)

1 answer

✓ Accepted answer

You could make the NPC's health to 101, and then do this:

local sound = Instance.new("Sound", Workspace)
sound.Pitch = 1 
sound.SoundId = "rbxassetid://902587196" 
sound.Volume = 1 
sound.Looped = false 
local playonce = false

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")

	if humanoid then
		if humanoid.Health == 100 then
			if playonce == false then
				playonce = true
				sound:Play()
			end
		end
	end	
end)

2 comments

0

This might be a very bad way and I think there is more complicated and better answers, but I hope this helps! Thanks for reading!

0

Wow. I never thought of that. That was so smart. Thanks for your help!

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