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 8 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!

01local music ={123456789,123456789,123456789,123456789,123456789,123456789,123456789}
02local MusicList =  music[math.random(#music)]
03local P = script.Parent.Parent
04Enabled = true
05 
06function onTouched(hit)
07 
08if (Enabled == true) then
09Enabled = false
10MusicList:Play()
11P.Name = ""
12wait()
13P.Name = ""
14Enabled = true
15end
16end
17 
18script.Parent.Touched:connect(onTouched)

3 answers

Log in to vote
0
Answered by 8 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.

01local sound = Instance.new("Sound") --Make's sound.
02local randommusic = math.random(1,7) -- because you have 7 sounds.
03local part = script.Parent.Parent
04local Enabled = true
05 
06part.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
View all 22 lines...
0
You should not keep adding if's. Use elseif's. FiredDusk 1466 — 8y
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 — 8y
0
I'm also new to scripting, sorry BlackOrange3343 2676 — 8y
Ad
Log in to vote
1
Answered by
cfiredog 274 Moderation Voter
8 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:

1local sound = Instance.new("Sound", --[[ set parent here --]] ) -- Creates a new sound object
2sound.SoundId = "http://www.roblox.com/asset/?id=" .. MusicList -- Sets the sound ID
3sound:Play() -- Plays the sound
Log in to vote
0
Answered by 8 years ago
1local song,sound=math.random(1,7),Instance.new("Sound",script.Parent.Parent)
2script.Parent.Parent.Touched:Connect(function(hit)
3       sound.SoundId="rbxassetid://"..music[song]
4       sound:Play()
5end)
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 — 8y

Answer this question