How can I make it make a sound when you sell

Hello! How can I make this Script make a sound when you sell? But when you can’t sell anything, it does not make a sound.

Script:

local Part = script.Parent
Part.Touched:Connect(function(HIT)
	local H = HIT.Parent:FindFirstChild("Humanoid")	
	if H then
		local player = game.Players:GetPlayerFromCharacter(HIT.Parent)	
if player then
	local leaderstats = player:WaitForChild("leaderstats")
	local Currency = leaderstats.Money
	local Selling = leaderstats.Coins
		if Selling.Value > 0 then
			Currency.Value = Currency.Value + Selling.Value *1 
			Selling.Value = 0
		end
end
end
end)

Any help will be appreciated! :grinning_face_with_smiling_eyes:

Its really easy to do this you just need a sound you want to play the script would look like this -

local Part = script.Parent
Part.Touched:Connect(function(HIT)
	local H = HIT.Parent:FindFirstChild("Humanoid")	
	if H then
		local player = game.Players:GetPlayerFromCharacter(HIT.Parent)	
if player then
	local leaderstats = player:WaitForChild("leaderstats")
	local Currency = leaderstats.Money
	local Selling = leaderstats.Coins
		if Selling.Value > 0 then
			Currency.Value = Currency.Value + Selling.Value *1 
			Selling.Value = 0
            local Sound = Instance.new("Sound",HIT.Parent)
            Sound.SoundId = "rbxassetid://" -- Whatever the sound id you want to play is goes here
            Sound:Play()
            game.Debris:AddItem(Sound,1)
		end
end
end
end)

Do I need to change anything in there besides the Audio ID Number?

just chance the id and you should be set!

Well I keep getting this error?

try this

local Part = script.Parent
Part.Touched:Connect(function(HIT)
	local H = HIT.Parent:FindFirstChild("Humanoid")	
	if H then
		local player = game.Players:GetPlayerFromCharacter(HIT.Parent)	
if player then
	local leaderstats = player:WaitForChild("leaderstats")
	local Currency = leaderstats.Money
	local Selling = leaderstats.Coins
		if Selling.Value > 0 then
			Currency.Value = Currency.Value + Selling.Value *1 
			Selling.Value = 0
local sound = game.lighting.SoundName --add ur sound into lighting and put the name here
sound:Play()
		end
end
end
end)

So I don’t get the error anymore, I don’t hear it though.

It needs to be “rbxassetid://(idhere)”

What do you mean? Are you talking about the ID Inside the Audio, cause I already have that. What do you exactly mean. Where are you talking about? (oh wait one second)

Okay, I fixed it and it worked! Thank you so much, I appreciate it!

1 Like
local sound = workspace.sound --change sound to name of the sound instance
local Part = script.Parent
Part.Touched:Connect(function(HIT)
	local H = HIT.Parent:FindFirstChild("Humanoid")	
	if H then
		local player = game.Players:GetPlayerFromCharacter(HIT.Parent)	
		if player then
			local leaderstats = player:WaitForChild("leaderstats")
			local Currency = leaderstats.Money
			local Selling = leaderstats.Coins
			if Selling.Value > 0 then
				sound:Play()
				Currency.Value = Currency.Value + Selling.Value *1 
				Selling.Value = 0
			end
		end
	end
end)

Much simpler way of achieving the same result. I’ve also fixed the formatting.

This would work but game.lighting should be game.Lighting.

I ended up using this script

local Part = script.Parent
Part.Touched:Connect(function(HIT)
	local H = HIT.Parent:FindFirstChild("Humanoid")	
	if H then
		local player = game.Players:GetPlayerFromCharacter(HIT.Parent)	
if player then
	local leaderstats = player:WaitForChild("leaderstats")
	local Currency = leaderstats.Money
	local Selling = leaderstats.Coins
		if Selling.Value > 0 then
			Currency.Value = Currency.Value + Selling.Value *1 
			Selling.Value = 0
            local Sound = Instance.new("Sound",HIT.Parent)
            Sound.SoundId = "rbxassetid://" -- Whatever the sound id you want to play is goes here
            Sound:Play()
            game.Debris:AddItem(Sound,1)
		end
end
end
end)
1 Like

I know, it’s just a more complicated way of achieving the same thing.