Trouble with Coin Sound

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to know how to solve this problem.

  2. What is the issue? Include screenshots / videos if possible!
    I have a coin when you touch it, it plays a sound and then destroy. But somehow it doesn’t work
    robloxapp-20230814-1325177.wmv (442.8 KB)
    image

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    When I remove the :Destroy() it does work but when I added it again the sound doesn’t play
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local coin = script.Parent
local touchPart = script.Parent.Coin

local debounce = false

touchPart.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if plr then
		
		if debounce == false then
			script.Parent.Sound:Play()
			debounce = true
		end

	end
	script.Parent:Destroy()
end)
18 Likes

You want that the sound play again ?

7 Likes

I want the sound play when I touch the coin. But when I touch it the coin disappear without playing the sound

5 Likes

Add a wait before destroying the model

6 Likes

It doesn’t work

local coin = script.Parent
local touchPart = script.Parent.Coin

local debounce = false

touchPart.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if plr then
		
		if debounce == false then
			script.Parent.Sound:Play()
			script.Parent.Sound:Stop()
			debounce = true
		end
		
	end
	wait(2)
	script.Parent:Destroy()
	
end)
4 Likes

Why use a debounce here anyway?

The issue is you are destroying the coin instantly, which has the sound in it so the sound is destroyed too.

4 Likes

Don’t make the sound stop remove the

4 Likes

Remove the stop event as the sound will just stop automatically when its done.

4 Likes

Even if I am not using the debounce, the sound isn’t play

local coin = script.Parent
local touchPart = script.Parent.Coin

local debounce = false

touchPart.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if plr then
		
			script.Parent.Sound:Play()

	end

	script.Parent:Destroy()
	
end)
5 Likes

the model is being destroying just after the sound is playing write :

touchPart.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

	if plr then

		script.Parent.Sound:Play()
		script.Parent.Sound.Ended:Wait()
		script.Parent:Destroy()
	end

end)
5 Likes

Play the sound,
task.wait(however long the sound is)
Destroy the Coin, but don’t do script.Parent:Destroy() because that destroys the Model.

6 Likes

oh It worked, but the sound is repeated for a while. Is debounce needed?

4 Likes

Yeah you need a debounce. (Ignore this)

6 Likes

I don’t know why but the sound still repeat 4 times and destroyed when I have my debounce (sorry I am new)

local coin = script.Parent
local touchPart = script.Parent.Coin

local debounce = false

touchPart.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if plr then
		
		if debounce == false then
			script.Parent.Sound:Play()
			script.Parent.Sound.Ended:Wait()
			debounce = true
		end
		script.Parent:Destroy()
		
	end
	
end)
4 Likes

Write like this :

touchPart.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

	if plr and not debounce then
		debounce = true

		script.Parent.Sound:Play()
		script.Parent.Sound.Ended:Wait()
		script.Parent:Destroy()
		
		debounce = false
	end

end)
5 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.