ProximityPrompt Random Sound

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

image

1 Like
local coinsound = script.Parent.Parent.Insert


local Part = script.Parent.Parent
local ProximityPrompt = script.Parent

local RandomSound = math.random(5)

ProximityPrompt.Triggered:Connect(function(Player)
	coinsound:Play()
	wait(1)
	local sound = script.Parent.Parent["SoundName"..tostring(RandomSound)]
	print(sound.Name..' played')
	ProximityPrompt.Enabled = false
	wait(sound.TimeLength)
	ProximityPrompt.Enabled = true
end)

This issue was you were referencing ‘switch’, which was stored as a boolean value (ProximityPrompt.Enabled). Instead you needed to do ProximityPrompt.Enabled = true/false

3 Likes

Oh alright, thank you. You also shortened the script a lot, I way overcomplicated it.

How would I make the math.random number choose another random number after the function is played? I’m testing this now and I seem to only be getting the same sound played every time I trigger the proximityprompt.

That’s because he set RandomSound to math.random(5) outside of the function, meaning that every time he uses RandomSound it would be the same number as you’re not changing the value of RandomSound To change it i think you can just put

inside of the function, so it’d look something like

local coinsound = script.Parent.Parent.Insert


local Part = script.Parent.Parent
local ProximityPrompt = script.Parent

ProximityPrompt.Triggered:Connect(function(Player)
    local RandomSound = math.random(5)
	coinsound:Play()
	wait(1)
	local sound = script.Parent.Parent["SoundName"..tostring(RandomSound)]
	print(sound.Name..' played')
	ProximityPrompt.Enabled = false
	wait(sound.TimeLength)
	ProximityPrompt.Enabled = true
end)