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

Only 1 sound plays even though it's randomized?

So I successfully made my velocity script, but it only plays 1 sound and no matter how many times I try it still plays that 1 sound.

local soundTable =  {script.Sounds.Sound1, script.Sounds.Sound2, script.Sounds.Sound3} -- Make sure to seperate them using commas, and start it with "script.Sounds." if you are gonna add another sound.
local randomSound = soundTable[math.random(#soundTable)]

script.Parent.Touched:Connect(function()
	if script.Parent.Velocity.Magnitude >= 1 then
		randomSound:Play()
	end
end)

"Sound2" plays.

1 answer

✓ Accepted answer

This is because you're picking a random sound when the script first runs, so it'll only pick one sound and because it's not inside the Touched event, it won't choose another one, it'll just remain on the same one.

You'd have to put the variable inside the touched event.

local soundTable =  {script.Sounds.Sound1, script.Sounds.Sound2, script.Sounds.Sound3}

script.Parent.Touched:Connect(function()
    if script.Parent.Velocity.Magnitude >= 1 then
		local randomSound = soundTable[math.random(#soundTable)]
        randomSound:Play()
    end
end)

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