Hello, I've got a block in my Lobby that I want to play random music when touched. I think I'm close to getting this right, but I've misidentified the second line:
00:48:47.197 - Workspace.Lobby.TouchToPlayAudio.Touch to Play Audio:10: attempt to index upvalue 'MusicList' (a number value)
Any thoughts on how to get this going correctly? :) Thanks!
01 | local music = { 123456789 , 123456789 , 123456789 , 123456789 , 123456789 , 123456789 , 123456789 } |
02 | local MusicList = music [ math.random(#music) ] |
03 | local P = script.Parent.Parent |
04 | Enabled = true |
05 |
06 | function onTouched(hit) |
07 |
08 | if (Enabled = = true ) then |
09 | Enabled = false |
10 | MusicList:Play() |
11 | P.Name = "" |
12 | wait() |
13 | P.Name = "" |
14 | Enabled = true |
15 | end |
16 | end |
17 |
18 | script.Parent.Touched:connect(onTouched) |
The answer up there was correct, but he didn't explain how to play "random" music. So I will be helping.
You are trying to play ID's which is not possible. You need a sound in order to do that.
01 | local sound = Instance.new( "Sound" ) --Make's sound. |
02 | local randommusic = math.random( 1 , 7 ) -- because you have 7 sounds. |
03 | local part = script.Parent.Parent |
04 | local Enabled = true |
05 |
06 | part.Touched:connect( function (Hit) --When Touched |
07 | if Enabled = = true then |
08 | Enabled = false |
09 | if randommusic = = 1 then |
10 | sound.SoundId = "http://www.roblox.com/asset/?id=1234567" |
11 | sound:Play() |
12 | if randommusic = = 2 then |
13 | sound.SoundId = "http://www.roblox.com/asset/?id=1234567" |
14 | sound:Play() |
15 | if randommusic = = 3 then |
In your current script, you are trying to play a sound ID. At line 10, MusicList:Play()
is the equivalent of saying 123456789:Play()
. To fix your problem, you need to create a sound object and assign the ID value to that object:
1 | local sound = Instance.new( "Sound" , --[[ set parent here --]] ) -- Creates a new sound object |
2 | sound.SoundId = "http://www.roblox.com/asset/?id=" .. MusicList -- Sets the sound ID |
3 | sound:Play() -- Plays the sound |
1 | local song,sound = math.random( 1 , 7 ),Instance.new( "Sound" ,script.Parent.Parent) |
2 | script.Parent.Parent.Touched:Connect( function (hit) |
3 | sound.SoundId = "rbxassetid://" ..music [ song ] |
4 | sound:Play() |
5 | end ) |