Attempt to index number with 'Play'

  1. What do you want to achieve?
    I’m trying to make a random sound play

  2. What is the issue?
    I get this "Attempt to index number with ‘Play’ " every time.

   local sounds = script.thunderSounds:GetChildren()
	local ChosenSound = math.random(1,#sounds)
	ChosenSound:Play()

You are generating a random number here

local ChosenSound = math.random(1,#sounds)

so ChosenSound could be 1, 2, 3, or 4 etc.

So when you are saying ChosenSound:Play()
You are actually saying 1:Play() or 4:Play()
Which is not what you want to do. Instead, with that randomly generated number, you can index the sounds table and grab the corresponding sound object

local ChosenSound = sounds[math.random(1,#sounds)]
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.