Hello! I'm just learning how to script. Someone helped me while I was making a script for a button that would change the song playing every time it was pressed, and I learned quite a lot from that!! I'm now trying to modify the script so that it randomizes what song plays when the button is pressed, and also make it so the button can only be pressed every 30 seconds.
Here is my script after i've recieved help from the accepted answer below!!
local songs = {
"http://www.roblox.com/asset/?id=2206587324",
"http://www.roblox.com/asset/?id=4737555180",
"http://www.roblox.com/asset/?id=2231782710",
"http://www.roblox.com/asset/?id=2538961789"
}
local songLength = #songs
local Sound = script.Parent.Parent.Sound
local CD = script.Parent
local randomGenerator = Random.new()
local selected = 0
Sound.Pitch = 1
Sound.Volume = 3
Sound.Name = "Player"
song1 = "http://www.roblox.com/asset/?id=2538961789" --paste last song id here
Sound.SoundId = song1
Sound:play()
enabled = true
CD.MouseClick:Connect(function()
if not enabled then
return
end
enabled = false
selected = randomGenerator:NextInteger(1, #songs)
local chosen = selected%#songs
if chosen == 0 then
chosen = #songs
end
print(chosen)
Sound.SoundId = songs[chosen]
Sound:Play()
Sound.Looped = true
wait(30)
enabled = true
end)
Here is the original script:
local songs = {
"http://www.roblox.com/asset/?id=2206587324",
"http://www.roblox.com/asset/?id=4737555180",
"http://www.roblox.com/asset/?id=2231782710",
"http://www.roblox.com/asset/?id=2538961789"
}
local songLength = #songs
local Sound = script.Parent.Parent.Sound
local CD = script.Parent
local selected = 0
Sound.Pitch = 1
Sound.Volume = 3
Sound.Name = "Player"
song1 = "http://www.roblox.com/asset/?id=2538961789" --paste last song id here
Sound.SoundId = song1
Sound:play()
CD.MouseClick:Connect(function()
selected += 1
local chosen = selected%#songs
if chosen == 0 then
chosen = #songs
end
print(chosen)
Sound.SoundId = songs[chosen]
Sound:Play()
Sound.Looped = true
end)
here is my modified version that doesn't work :( the output states: (Workspace.Model.Button.ClickDetector.Script:19: attempt to perform arithmetic (mod) on table and number) --I sort of understand this, but I don't know how else to make it Random
local songs = {
"http://www.roblox.com/asset/?id=2206587324",
"http://www.roblox.com/asset/?id=4737555180",
"http://www.roblox.com/asset/?id=2231782710",
"http://www.roblox.com/asset/?id=2538961789"
}
local songLength = #songs
local Sound = script.Parent.Parent.Sound
local CD = script.Parent
local selected = 0
Sound.Pitch = 1
Sound.Volume = 3
Sound.Name = "Player"
song1 = "http://www.roblox.com/asset/?id=2538961789" --paste last song id here
Sound.SoundId = song1
Sound:play()
CD.MouseClick:Connect(function()
selected = Random --changed += 1 to Random but it just makes the button not work...
local chosen = selected%#songs
if chosen == 0 then
chosen = #songs
end
print(chosen)
Sound.SoundId = songs[chosen]
Sound:Play()
Sound.Looped = true
wait(30) --added wait but doesnt do anything
end)

1 comment
thank you