Trouble looping the change of a variable

Hello, today i have been experiencing issues with a on death sound.
Im trying to make it so when you die it plays a custom audio instead of the default.
I have an issue with this though, it rather plays one audio and not random or it just doesnt play.
I am back to scripting after around ~ 1 year so im not up to my best.

Here is the code:

local Sounds = script:GetChildren(“Sound”)
local RandomIndex = math.random(1,#Sounds)
local RandomSound = Sounds[RandomIndex]
local asound = nil

local function findsound()
Sounds = script.Folder:GetChildren(“Sound”)
RandomIndex = math.random(1,#Sounds)
RandomSound = Sounds[RandomIndex]
asound = RandomSound.SoundId
end

while true do
wait()
findsound()
end

local SOUND_DATA : { [string]: {[string]: any}} = {
Died = {
SoundId = asound,
},


I have a folder in the script with the audios

I hope you can help me.

1 Like