Touched event malfunctioning

Hello devs,
so when i was working with touched events i noticed that they are malfunctioning.
i put this script inside a part:

--anirudh851
script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		script.Parent.Drumming:Play()
	end
end)

altho the sound does play when u touch the part, it plays only until ur standing still. as soon as u start moving (but ur still on the part) the sound stops playing for an unknown reason. any way to cope up with this?

It’s because the touched event happens multiple times, so it will replay the sound over and over again. With the humanoid check, also check if the audio is not playing

--anirudh851
local part = script.Parent
local drumming = part.Drumming

part.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid and not drumming.IsPlaying then
		drumming:Play()
	end
end)
2 Likes

Woah thanks ur a lifesaver bruh. it worked so nice thanks a lot, have a great day ahead! Thanks for helping again or I would have got fired lol :grimacing:

1 Like