Im trying to make a radio on a npc that play’s random music, but the actual thing is that i do not want it to play the same music.
How would i do that?
Im trying to make a radio on a npc that play’s random music, but the actual thing is that i do not want it to play the same music.
How would i do that?
One thing you can do is store the played music in a table, then once you chose a new one make sure it’s not in the table (use table.find
)
local LastSong
local RandomSong
function GetRandomSong() -- Gets a random Song
local music = folder:GetChildren()
local Random = math.random(1, #music)
return RandomSong = music[Random]
end
function CheckSong() -- Checks if song is the same as the last one.
local Song = GetRandomSong()
if Song ~= LastSong then
LastSong = Song
LastSong:Play()
LastSong.Finished:Connect(CheckSong)
end
end
I’m not sure if this will work, let me know.
I don’t have time to explain how this works, but here’s a music system from my game, I call it ‘random feed system’.
There are over 60 tracks in my game, but even with 20 tracks, it’d still feel like there’s a lot more.
If you understand programming, you should be able to implement a system like this in your game.
P.S. each sound in the sounds folder should have a unique name.
math.randomseed(tick())
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local spring = require(script.Parent.Packages.Spring)
local audios = script.Parent.Audio:GetChildren()
local feed = {}
local function getAudio()
return audios[math.random(1, #audios)]
end
task.spawn(function()
while true do
local current = getAudio()
if table.find(feed, current.Name) then
repeat current = getAudio() task.wait() until not table.find(feed, current.Name)
end
table.insert(feed, current.Name)
if #feed > math.floor(#audios/2) then
table.remove(feed, 1)
end
local volume = current.Volume
current.Volume = 0
current:Play()
spring.target(current, 1, 0.2, { Volume = volume })
current.Ended:Wait()
end
end)
Thank you! This works perfectly.