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!
local music ={123456789,123456789,123456789,123456789,123456789,123456789,123456789} local MusicList = music[math.random(#music)] local P = script.Parent.Parent Enabled = true function onTouched(hit) if (Enabled == true) then Enabled = false MusicList:Play() P.Name = "" wait() P.Name = "" Enabled = true end end 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.
local sound = Instance.new("Sound") --Make's sound. local randommusic = math.random(1,7) -- because you have 7 sounds. local part = script.Parent.Parent local Enabled = true part.Touched:connect(function(Hit) --When Touched if Enabled == true then Enabled = false if randommusic == 1 then sound.SoundId = "http://www.roblox.com/asset/?id=1234567" sound:Play() if randommusic == 2 then sound.SoundId = "http://www.roblox.com/asset/?id=1234567" sound:Play() if randommusic == 3 then sound.SoundId = "http://www.roblox.com/asset/?id=1234567" sound:Play() --And so on. end end end end)
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:
local sound = Instance.new("Sound", --[[ set parent here --]] ) -- Creates a new sound object sound.SoundId = "http://www.roblox.com/asset/?id=" .. MusicList -- Sets the sound ID sound:Play() -- Plays the sound
local song,sound=math.random(1,7),Instance.new("Sound",script.Parent.Parent) script.Parent.Parent.Touched:Connect(function(hit) sound.SoundId="rbxassetid://"..music[song] sound:Play() end)