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

How Do I Pull Numbers From An Array? (script.Parent.Sounds[math.random(1, 16)]:Play() not working)

Sounds = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17"}

function Music()
local notes = math.random(5, 10)
for i = 1, notes do
script.Parent.Sounds[math.random(1, 16)]:Play()
wait(.5)
end
end

Music()

Error Message sounds is not a valid member of Model.

1 answer

✓ Accepted answer

I assume what you have are sounds inside of a part that are named "2", "3", "4", etc. First you have to find the number in the array, this can be done by picked a random number(1 - 16) and then searching for the number that is in that spot

For Instance, if I do math.random(1, 16) and a three appears, '4' is the picked number because it is the third variable in the array

Here is my finished script:

Sounds = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17"}

function Music()
	local notes = math.random(5, 10)
	for i = 1, notes do
		local num = math.random(1, 16)
		local name = Sounds[num]
		print(num, name)
		if name and script.Parent:FindFirstChild(name) then
			script.Parent:FindFirstChild(name):Play()
		end
		wait(.5)
	end
end

wait(1)
Music()

If you notice, when run the 'print' at line 8 returns the number, then the variable that is in the place of the number in the table

Hope this helped you!

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