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.
And the number randomizer script. (This one is working well. The value is randomly changing.)
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
UIS.InputBegan:Connect(function(key, gp)
if key.KeyCode == hotkey then
local RandomMath = Random.new():NextInteger(1, 3)
workspace["Click"..RandomMath]:Play()
end
end)
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
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.
okay so here is the script and now Im going to run it.