Picking a new sound from a folder at random

Found some code on the DevForum that does pick a new sound every time but only picks the same sound when randomly picked and does not variate.
code:

local Lighting = game.Lighting
local Ambient = Lighting.OutdoorAmbient
local Clouds = game.Workspace.Terrain.Clouds
local WeatherSounds = game.Workspace.WeatherSounds
local TS = game:GetService("TweenService")


local Sound = {}
for i,v in pairs(WeatherSounds:GetChildren()) do
	if v:IsA("Sound") then
		table.insert(Sound,v)
	end
end
local RandomSound = Sound[math.random(1,#Sound)]


local CloudTweenLight = TS:Create(Clouds, TweenInfo.new(1,Enum.EasingStyle.Elastic,Enum.EasingDirection.Out), {Color = Color3.new(1, 1, 1)})
local CloudTweenDark = TS:Create(Clouds, TweenInfo.new(1,Enum.EasingStyle.Elastic,Enum.EasingDirection.Out), {Color = Color3.new(0.0980392, 0.0980392, 0.0980392)})
local AmbientTweenLight = TS:Create(Lighting, TweenInfo.new(1,Enum.EasingStyle.Elastic,Enum.EasingDirection.Out), {OutdoorAmbient = Color3.fromRGB(118, 118, 118)})
local AmbientTweenDark = TS:Create(Lighting, TweenInfo.new(1,Enum.EasingStyle.Elastic,Enum.EasingDirection.Out), {OutdoorAmbient = Color3.fromRGB(64, 64, 64)})
		while true do 
		Clouds.Color = Color3.new(0.0980392, 0.0980392, 0.0980392)
		Lighting.Ambient = Color3.fromRGB(64, 64, 64)
		wait(math.random(0, 10))
		RandomSound.Playing = true
		CloudTweenLight:Play()
		AmbientTweenLight:Play()
		wait(2)
		CloudTweenDark:Play()
		AmbientTweenDark:Play()
		wait(2)
		wait(math.random(0, 2))
end


image

This is easily solved, actually.

In the context of my game, I made a separate function for playing sounds, and in the function, it would first setting the “previousSound” to nil, then get the folder of sounds (using GetChildren()), then use “repeat until” with a math.random inside which would be skipped on the first use, then after that set the “previousSound” variable to the selected sound, so that the “repeat until” will keep choosing a sound until it’s not the same sound as before

it would look something like this:

function playSound()

	local sounds = Sounds:GetChildren()
	repeat local sound = sounds[math.random(1, #sounds)] -- makes sure it's not the same sound as before
	until sound ~= soundnumber

	soundnumber = sound -- sets it for the next time a sound is to be played

	sound:Play()

end

I would also suggest changing the way your sounds are organized.

1 Like

I don’t really see any problems with his sound organization, the only thing I would actually change would be putting them inside a SoundGroup instead, but I would only do that if I wanted a volume slider for weather sounds.

edit: ok there is 1 problem, they are grouped under workspace, he should probably move it to sound service so it plays at the same volume from anywhere assuming he wants to play it in multiple places

2 Likes

I propose a soft rewrite of your script here.

local Lighting = game.Lighting
local Ambient = Lighting.OutdoorAmbient
local Clouds = game.Workspace.Terrain.Clouds
local WeatherSounds = game.Workspace.WeatherSounds
local TS = game:GetService("TweenService")
local CloudTweenLight = TS:Create(Clouds, TweenInfo.new(1,Enum.EasingStyle.Elastic,Enum.EasingDirection.Out), {Color = Color3.new(1, 1, 1)})
local CloudTweenDark = TS:Create(Clouds, TweenInfo.new(1,Enum.EasingStyle.Elastic,Enum.EasingDirection.Out), {Color = Color3.new(0.0980392, 0.0980392, 0.0980392)})
local AmbientTweenLight = TS:Create(Lighting, TweenInfo.new(1,Enum.EasingStyle.Elastic,Enum.EasingDirection.Out), {OutdoorAmbient = Color3.fromRGB(118, 118, 118)})
local AmbientTweenDark = TS:Create(Lighting, TweenInfo.new(1,Enum.EasingStyle.Elastic,Enum.EasingDirection.Out), {OutdoorAmbient = Color3.fromRGB(64, 64, 64)})
local Sounds = WeatherSounds:GetChildren()


local function startLoop()

while not false do
local newSound = Sounds[math.random(1, #Sounds)]
		Clouds.Color = Color3.new(0.0980392, 0.0980392, 0.0980392)
		Lighting.Ambient = Color3.fromRGB(64, 64, 64)
		task.wait(math.random(0, 10))
		newSound:Play()
		CloudTweenLight:Play()
		AmbientTweenLight:Play()
		task.wait(2)
		CloudTweenDark:Play()
		AmbientTweenDark:Play()
		task.wait(2 + (math.random(0,2))
end

end


startLoop()

This should more or less function the same as your current script but would fix the sound being the same every time - the reason why the sound is the same in your current setup is because you’re selecting the random sound once outside of the loop, so while indeed you do get a random sound, you only get 1 random sound.

I removed your “for i, v in pairs” loop as it is unnecessary, everything in your “WeatherSounds” folder should be a sound object, so there’s no need to loop over them.

I also do recommend moving the WeatherSounds folder to ReplicatedStorage, but honestly it’s my opinion that it doesn’t matter that much.

(Setting the value of Clouds.Color and Lighting.Ambient at the top of the loop is not necessary actually… :P)

2 Likes

Tried to fix an error I got but I am not the most tech-savvy.
19:17:24.939 Players.WaterCanxiety.PlayerScripts.lightning:28: Expected ')' (to close '(' at line 26), got 'end' - Studio - lightning:28

1 Like

Whoops! :3c

Change this at the bottom of the loop:

task.wait(2 + (math.random(0,2))

To this:

task.wait(2 + (math.random(0,2)))

(T’was a missing parentheses!!)

1 Like

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