I attempted to develop a script that would randomly select and play a sound. This is essentially for a Juke Box. I also tried to deactivate the proximity prompt while the music was playing. This, however, did not work out for me. I know I’ve been going about this the incorrect way, but I can’t seem to figure out what the best course of action would be to make this happen.
This is my horrible code, (I know I used the math.random wrong because it will not refresh after the number has been chosen.) That’s my main issue.
local sound1 = script.Parent.Parent.SoundName1
local sound2 = script.Parent.Parent.SoundName2
local sound3 = script.Parent.Parent.SoundName3
local sound4 = script.Parent.Parent.SoundName4
local sound5 = script.Parent.Parent.SoundName5
local coinsound = script.Parent.Parent.Insert
local Part = script.Parent.Parent
local ProximityPrompt = script.Parent
local switch = ProximityPrompt.Enabled
local RandomSound = (math.random(5))
ProximityPrompt.Triggered:Connect(function(Player)
coinsound:Play()
wait(1)
if RandomSound == 1 then
print("1 played")
sound1:Play()
switch = false
wait(107.76)
switch = true
end
if RandomSound == 2 then
print("2 played")
sound2:Play()
switch = false
wait(52.853)
switch = true
end
if RandomSound == 3 then
print("3 played")
sound3:Play()
switch = false
wait(60)
switch = true
end
if RandomSound == 4 then
print("4 played")
sound4:Play()
switch = false
wait(119.088)
switch = true
end
if RandomSound == 5 then
print("4 played")
sound5:Play()
switch = false
wait(30.484)
switch = true
end
end)
This is what’s inside of my workspace