Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to randomize what a button does and also add a wait?

Asked by 4 years ago
Edited 4 years ago

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 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

If you look at the documentation for Random, you'll see that it's used a little differently than the way you've used it.

Instead, at the top of your script, you need to create a random number generator:

local randomGenerator = Random.new()

Then, where you're setting selected, you can use the Random:NextInteger method:

selected = randomGenerator:NextInteger(1, #songs)

edit: Forgot about the wait part.

Your wait(30) solution didn't work because that event is going to run asynchronously, i.e. in parallel (sorta) to the rest of your code. So if you press the button once, wait ten seconds, and click it again, the handler from the first click will be waiting, but the handler from the second click doesn't care and will be executed immediately anyways.

What you're looking for is called a debounce. Here's a great article on it: https://developer.roblox.com/en-us/articles/Debounce

0
thank you LiLFriks 39 — 4y
Ad

Answer this question