How can I make my coin have a sound effect when somebody picks it up?

Alright, so, I’m trying to make my coin do a sound effect whenever a person picks it up, I don’t know the script that I’m supposed to put. These are my current scripts for the coin pickup;


function coinPickup(partTouching)
	print(partTouching)
	if partTouching.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(partTouching.Parent)
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
		print("COINS: " .. player.leaderstats.Coins.Value)
		script.Parent:Destroy()
	end
end

script.Parent.Touched:Connect(coinPickup)

Do I put the sound effect on another script or the same script?

1 Like
local Sound = script:FindFirstChild("Sound") -- change to where you want the sound to be

function coinPickup(partTouching)
	print(partTouching)
	if partTouching.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(partTouching.Parent)
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
		print("COINS: " .. player.leaderstats.Coins.Value)
                Sound:Play()
		script.Parent:Destroy()
	end
end

script.Parent.Touched:Connect(coinPickup)
2 Likes

Do I put it at the same script?

Yes, you put it at the same script. The main parts are; local Sound = script:FindFirstChild("Sound") and Sound:Play()

Thank you so much! I appreciate it.

1 Like

Well, I put the ID on the lines of scripts you gave me but now it doesn’t disappear and it still wont play the sound.

No, the Sound is a sound object, not the ID. API Reference here. You have to make a sound object and then put your sound ID into it.

I’m sorry but I still don’t understand, I don’t know what a “sound object” is, but with the "You have to make a sound object, do you mean I need to make a new part and put the script there? Wouldn’t that double the coin pickup?

No. It’s a roblox object. If you hover over your script, there will be a Plus Sign. Click it then type in “Sound” and then there will be a sound object. Click it and then it will be added under your script, add the soundID and then play it from the script.


Looks like this.

I did exactly what you said, I added the IDs to both of the lines of script and I added the sound into the script, but it still isn’t playing and the coin won’t disappear even if I touched it. The other coins I made disappears since I still haven’t changed the script or added a sound to them.

Okay, so what I am saying is, change The SoundID That Is Inside The Sound Object. do not add any sound ID’s inside of the script

It is inside of the Properties Tab when you click on the sound object,

I’m sorry if I’m being annoying but it still doesn’t play but it does disappear this time. Do I need to change something in the settings of the sound object?

Do you have a sound object with a sound ID defined? What is the sound a bell? Do you have the ID for the sound?

You should post your script now and a screenshot of your workspace objects.


function coinPickup(partTouching)
	print(partTouching)
	if partTouching.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(partTouching.Parent)
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
		print("COINS: " .. player.leaderstats.Coins.Value)
		Sound:Play()
		script.Parent:Destroy()
	end
end

script.Parent.Touched:Connect(coinPickup)

Script for coin pickup ^^^

local partspeed = 0.05

while wait() do

	part.CFrame = part.CFrame * CFrame.Angles(0,partspeed,0)

end

Script for the coin to spin ^^^

image
Audio ^^^

image
^^^ Coins (Only one coin has the script with the audios)

Try this?

local Sound = script:WaitForChild("Sound")

local function coinPickup(partTouching)
    print("Event fired")
	local Player = game.Players:GetPlayerFromCharacter(partTouching.Parent)
    print(Player)

    if Player then   
        Player.leaderstats.Coins.Value += 1
        script.Parent:Destroy()
        print("Coins: ", player.leaderstats.Coins.Value)
    end
end

script.Parent.Touched:Connect(coinPickup)

You didn’t define Sound before you try to :play() it. (as @JackscarIitt has shown) He has given a line of code that should make it work. You should have been getting an error about the variable Sound being nil.

It probably would have been easier to put the sound outside the script and coin part so you wouldn’t have to copy it into each one.

Try this script. Make a local script in StarterCharacterScripts:

local plr = game:GetService("Players"):GetPlayerFromCharacter(script.Parent)

local sound = Instance.new("Sound")
sound.Volume = 5

sound.SoundId = "rbxassetid://1508145469"

sound.Parent = script.Parent:WaitForChild("HumanoidRootPart")

local oldValue = plr.leaderstats.Coins.Value
plr.leaderstats.Coins.Changed:Connect(function(prop)
	if plr.leaderstats.Coins[prop] == plr.leaderstats.Coins.Value then
		
	if plr.leaderstats.Coins.Value > oldValue then
		sound:Play()
		oldValue = plr.leaderstats.Coins.Value
		end
	end
end)

The script doesn’t work, I put a sound object and the sound ID on the script