Math.random Refresh In a Function?

Under a ProximityPrompt, I have a script that plays a random sound. However, I used math.random to generate a random integer, which was then used to play the random sound within its parented model. The problem is that a random value is picked once and then never modified.

This is the script I’m currently using

local coinsound = script.Parent.Parent.Insert


local Part = script.Parent.Parent
local ProximityPrompt = script.Parent

local RandomSound = math.random(2)

ProximityPrompt.Triggered:Connect(function(Player)
	ProximityPrompt.Enabled = false
	coinsound:Play()
	wait(2)
	local sound = Part["SoundName"..tostring(RandomSound)]
	print(sound.Name..' played')
	sound:Play()
	wait(sound.TimeLength)
	ProximityPrompt.Enabled = true
end)

image

Put it inside the function to call the math.random function again and hence refresh it.


ProximityPrompt.Triggered:Connect(function(Player)
local RandomSound = math.random(2)
	ProximityPrompt.Enabled = false
	coinsound:Play()
	wait(2)
	local sound = Part["SoundName"..tostring(RandomSound)]
	print(sound.Name..' played')
	sound:Play()
	wait(sound.TimeLength)
	ProximityPrompt.Enabled = true
end)

Gee, duh. Thank you for reminding me of common sense, I really didn’t think of that, lol.