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

How to make sound play when touched by a person?

Asked by 3 years ago

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

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago

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)
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! Xapelize 2658 — 3y
0
Wow. I never thought of that. That was so smart. Thanks for your help! PhacYouBeech 17 — 3y
Ad

Answer this question