(mathrandom) Play random audio script doesn't work

Hello, so I wanted to write a script that will play a sound when you press a specific key.
I made some variants of the audio by changing their pitch. And I named them
Click2 and Click3. Everything looks fine I didn’t get any errors but somehow the script still doesn’t work.

image

And the number randomizer script. (This one is working well. The value is randomly changing.)
image

1 Like

Hey,
There is no reason to use either while true loops. If you want a better way to do it randomly, why not use math.random inside the click script?

UIS.InputBegan:Connect(function(key,gp)
	if key.KeyCode == hotkey then
		local RandomMath = math.random(1,3)
		if RandomMath == 1 then
			game.Workspace.Click1:Play()--Play the audio
		elseif RandomMath == 2 then--Use Elseif, instead of needless if statements
			game.Workspace.Click2:Play()
		elseif RandomMath == 3 then
			game.Workspace.Click3:Play()
		end
	end
end)

I got rid of the while loop in the UIS function. It wasn’t needed, unless you wanted to repeatedly play the sound every .01 seconds. If you want the sound to loop, turn on Sound.Looped. Then, you can fire Sound:Stop() to stop the sound.

Just so you know, you can insert codeblocks by doing ``` at the begging/end of your code on DevForum :slight_smile:

2 Likes
UIS.InputBegan:Connect(function(key, gp)
    if key.KeyCode == hotkey then
        local RandomMath = Random.new():NextInteger(1, 3)

        workspace["Click"..RandomMath]:Play()
    end
end)

you don’t really need the if statements

Yeah, you don’t. I was trying to keep @ylafie’s general code structure, so they could understand it easier if they were familiar with that way. But good point :+1:

1 Like

@D0RYU, @Infinite_Visions
Thank you both for your help guys. Oh and,

Sorry, I didn’t know this existed.

And so now, It’s hard to decide which I’ll mark as a solution.
Who wants it the most? LOL This is the only way.
Oh wait… With the help of a mathrandom script. We can let the script decide it itself.
image
okay so here is the script and now Im going to run it.

image
@Infinite_Visions lol gg.

1 Like

I believe you can mark multiple replies as solutions.

Really? Woah, okay let me try.

Nah, It didn’t work. lol Sorry.

I thought it was possible, perhaps not, my bad.

1 Like