Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can i get this random audio player script to work?

I was trying to make a script that plays random audio OnClicked and when i tested it, it didnt work. Can someone tell me how to fix the problem?

 hull = script.Parent.Parent.Hull
 audios = math.random(0.2)
function onClicked()
	script.Parent.CFrame = CFrame.new(4.927, 41.072, -23.83)
	script.Parent.Parent.ON.CFrame = CFrame.new(4.927, 30.672, -23.83)
	script.Parent.Parent.ON.SurfaceLight.Enabled = true
	script.Parent.Parent.ON.Script.Disabled = false
	script.Parent.Parent.Buttons.Sound:Play()
	if audios == 0.1 then
	hull.Static:Play()
	end
	if audios == 0.2 then
	hull.HN:Play()	
	end
	script.Disabled = true
	end

	script.Parent.ClickDetector.MouseClick:connect(onClicked)


1 answer

✓ Accepted answer

It's actually quite simple, all you need to do is put all your sounds in a Folder in the Workspace.

local MusicFolder = workspace.Music:GetChildren()
script.Parent.ClickDetector.MouseClick:Connect(function()
	local rand = math.random(#MusicFolder) --Selects random song
	MusicFolder[rand]:Play() --Plays random song
end)

This script basically gets the children (sounds) from the Folder, and picks a random sound using math.random and plays it. If this worked, please upvote and accept answer. If it didn't, please comment! Thanks!

3 comments

0

But, i need them to play inside the model but not in workspace.

0

Nevermind, i managed to make the audios play inside of the model.

0

Yes, the folder was just a sample on how you would store your sounds.

This discussion is preserved as it was. New answers, comments, and votes are closed.