Make multiple sounds not play at the same time when it detects that the tool has touched a Humanoid

Hey Devs, I’ve been creating a punch that can play sound effects if the tool has touched a Humanoid

I would like a cooldown that plays 1 sound every few milliseconds to seconds.
I’ve tried to put on debounce but still, no luck.

Here’s the script:

script.Parent.Touched:Connect(function(Otherpart)
		local humanoid = Otherpart.Parent:FindFirstChildWhichIsA("Humanoid")
		local punch = false
		if humanoid ~= nil then
			if not punch then
				punch = true
				local randsound	 = math.random(1,5)
				if randsound == 1 then
					script.Parent.Sound.SoundId = "rbxassetid://9117969892"
					script.Parent.Sound["Play sound"].Disabled = false
				end

				if randsound == 2 then
					script.Parent.Sound.SoundId = "rbxassetid://9117969687" 
					script.Parent.Sound["Play sound"].Disabled = false
				end

				if randsound == 3 then
					script.Parent.Sound.SoundId = "rbxassetid://3932506625"
					script.Parent.Sound["Play sound"].Disabled = false
				end

				if randsound == 4 then
					script.Parent.Sound.SoundId = "rbxassetid://9117969584"
					script.Parent.Sound["Play sound"].Disabled = false
				end

				if randsound == 5 then
					script.Parent.Sound.SoundId = "rbxassetid://9117969717"
					script.Parent.Sound["Play sound"].Disabled = false
					wait(1)
					punch = false
				end
			end	
		end
	end)

These if functions control a script that plays the Sound.
image
^
The white dot is the Sound.
The cyan dot controls whether it plays the sound or not.
This is the script that is displayed as the cyan dot:

script.Parent:Play()

script.Parent.Ended:Connect(function()

script.Disabled = true

end)

The red dot is the script that is displayed above or is the script that randomizes the SoundEffects.

1 Like

Firstly, put the local punch = false outside of the Touched event because the debounce will not work properly,

And you put the two lines that wait 1 second and set punch to false inside of the if randsound == 5 then if statement, putting them outside should fix it

Try this out

local punch = false

script.Parent.Touched:Connect(function(Otherpart)
	local humanoid = Otherpart.Parent:FindFirstChildOfClass("Humanoid")
	
	if humanoid ~= nil and not punch then
		punch = true
		local randsound = math.random(1,5)
		
		if randsound == 1 then
			script.Parent.Sound.SoundId = "rbxassetid://9117969892"
		end

		if randsound == 2 then
			script.Parent.Sound.SoundId = "rbxassetid://9117969687" 
		end

		if randsound == 3 then
			script.Parent.Sound.SoundId = "rbxassetid://3932506625"
		end

		if randsound == 4 then
			script.Parent.Sound.SoundId = "rbxassetid://9117969584"
		end

		if randsound == 5 then
			script.Parent.Sound.SoundId = "rbxassetid://9117969717"
		end
		
		script.Parent.Sound["Play sound"].Disabled = false
		task.wait(1)
		punch = false		
	end
end)

Edit: Forgot to change the wait to task.wait because of wait being outdated and worse than task.wait

2 Likes