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

How to make a Touched block play Random Music?

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

✓ Accepted answer

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)

3 comments

0

You should not keep adding if's. Use elseif's.

0

Thank you for the responses. Studio keeps telling me: Touched is not a valid member of Model and then doesn't play. =/

0

I'm also new to scripting, sorry

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)

1 comment

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?

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