Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a Touched block play Random Music?

Asked by 7 years ago

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)

3 answers

Log in to vote
0
Answered by 7 years ago

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)

0
You should not keep adding if's. Use elseif's. FiredDusk 1466 — 7y
0
Thank you for the responses. Studio keeps telling me: Touched is not a valid member of Model and then doesn't play. =/ Never2Humble 90 — 7y
0
I'm also new to scripting, sorry BlackOrange3343 2676 — 7y
Ad
Log in to vote
1
Answered by
cfiredog 274 Moderation Voter
7 years ago

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
Log in to vote
0
Answered by 7 years ago
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)
0
Thank you for the response, but I'm getting the same result from Studio as with the previous suggestion, "Touched is not a valid member of Model". Thoughts? Never2Humble 90 — 7y

Answer this question