Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

how do i make a sound play when a player has the right amount of currency?

hi, im trying to make a obby but i need a script for a different song to begin when the player has the right amount of currency (Stage) or more.

here is the script i came up with (its in the workspace)

if leaderstats.Stage.Value >= 10
	then Sound2:Play()
	then Sound1: Stop()
end

thanks in advance, from poopypigeon245

4 answers

You are on the right track but you need to check for the stage value only when it changes. What you have currently will only execute once. You can use the Changed event of the value to detect when the value changed. When the value changes, check if the value is high enough and if so, change the song.

leaderstats.Stage.Changed:Connect(function()
	if leaderstats.Stage.Value >= 10 then
		Sound1:Stop()
		Sound2:Play()
	end
end)

3 comments

0

the colon between connect and function is underlined in red and it wont work

0

I made a typo. It should be fixed now.

0

where am i supposed to insert the script? (it didn't work in workspace or startergui)

Its actually this

leaderstats.Stage.Value.Changed:Connect(function()
    if leaderstats.Stage.Value >= 10 then
        Sound1:Stop()
        Sound2:Play()
    end
end

NOTE: You only need one Sound object.

First of all, try making a table/metatable(I just call it table) with all the SoundId and amount of currency required:

local Sounds = {
	["123456789"] = 10,
	["987654321"] = 20,
	-- Etc..
}

Make a function(@Y0_dude) that get the amount of currency the player has when the amount changes:

leaderstats.Stage.Changed:Connect(function(Amount)
	-- Code, read on
end)

Inside the function would be a For loop that checks the things inside the table, changes the soundId corresponding to the value it is, and plays the Sound:

for i,v in pairs(Sounds) do
	if leaderstats.Stage.Value == v then
		Sounds.soundId = i
		Sounds:Play()
	end
end

If you put it all together, it should be like this:

local Sounds = {
	["123456789"] = 10,
	["987654321"] = 20,
	-- Etc..
}
leaderstats.Stage.Changed:Connect(function()
	for i,v in pairs(Sounds) do
		if leaderstats.Stage.Value == v then
			Sounds.soundId = i
			Sounds:Play()
		end
	end
end)

2 comments

0

so i have the last script you wrote in soundservice. where do i move it and do i need to write the other scripts anywhere?

0

You need to move the script where Server Script can be executable: ServerScriptStorage or Workspace.

I'll give it a try and help you.

Try the code here down:

--You didn't define these variables, so I created them for you, and set some examples. Please use the correct path.
local Sound1 = game.Workspace.Sound1
local Sound2 = game.Workspace.Sound2

game.Players.PlayerAdded:connect(function(player)
	if player.leaderstats.Stage.Value > 9 then --I could put >= 10 but honestly I prefer > 9, and I never trusted >= and <= signals in ROBLOX, I find them unefficient, and they won't work for me, sometimes.
		Sound1:Stop()
		Sound2:Play()
	end
end)

--Oh and I'm not sure if it should, but since it's an obby, perhaps you want to put 'Looped' bool on each of the sounds. Nvm, this has nothing to do with the scripting.

2 comments

0

it didn't work. am i supposed to put the script in workspace or somewhere else?

0

It should. Put a Script not a LocalScript, in the workspace. Then make sure the player has the "Stage" leaderstats and also, set the Sound1 and Sound2 variables there up. Then it should work.

This discussion is preserved as it was. New answers, comments, and votes are closed.