Random table not working correctly

So currently I’ve got a script set up to play a taunt sound whenever the player presses the key bind, and have it play a sound picked out at random from a table.
Problem is, once the table has picked out a sound it’ll only play that sound for the taunt and no other sounds.

As an example, it’ll pick a random sound to play, but once its picked it will refuse to play any other sound, like sound2 has been picked, it’ll refuse to play any other taunt sound from the table that isn’t sound2.

Please keep in mind I’m very new to using tables and haven’t had much experience working with them in the past, I’m sure the issue is fairly basic.

Script

Soundtab = { }
Soundtab[1] = "rbxassetid://6345613019"
Soundtab[2] = "rbxassetid://Exmaple"
Soundtab[3] = "rbxassetid://Exmaple"
Soundtab[4] = "rbxassetid://Example"



  local taunt = Instance.new("Sound")
taunt.SoundId = (Soundtab[math.random(1,4)])

taunt.Volume = 5 --sets the volume of the taunt
taunt.Pitch = 1 --Sets the pitch of the taunt
taunt.Parent = hrp --Parents the humanoid root part```
1 Like

It is because you are picking a random number only once. You should pick it everytime the key is pressed. Move the taunt.SoundID right above the taunt:Play().

1 Like

Sorry for the late reply, forgot to say thank you!
This has been bugging me for a while and honestly learning tables is something I couldn’t do alone (:

1 Like

Before I continue, @Intencidy has answered your post, I’m only adding that you don’t have to create new sound instance every time you switch sound IDs, because that would be a complete waste of resources. Create one sound instance at the start instead, and simply change it’s sound ID (that’s what Dav_itt said between the lines as well, so I’m only pointing it out).

2 Likes