Hey, I'm completely new to Roblox Studio scripting,(And this site, this is my first post so excuse bad format please) and of course I'm no good. I've made some very basic scripts and my first test-project is to make a kind of timed bomb. One part of the code involves changing from one bleep sound per second to two bleeps per second, to three bleeps per seconds, etc... as the timer gets closer and closer to 0. However, it only seems capable of setting it to one and two bleeps per second. It does not change from two to three. My code is likely badly written and probably would create lag but that's not what I'm asking the solution to, so please don't comment about badly written code unless it is relevant. Please note how new I am to Roblox Studio. I won't know what many things are.
General Information:
I am testing in Roblox Studio's solo play - test system. I don't really mind if it does not work in an online game, this is just practise. These are regular scripts, and they are just inside a model inside a part in the workspace. No Lua errors appear however it does not work so it is technically still an error, no? The expected outcome is that the bomb beeps once per second above 25 seconds left, twice per second from 24 to 15, thrice per second from 14 to 10, etc... The outcome is that it beeps once per second above 25 seconds, and twice per second 24 to 0. (0 would be where it either explodes or hits a Critical Error, which is irrelevant to this question.) . I hope this information proves beneficial.
Things inside my bomb's model:
I have the main visual model which I assume is irrelevant as it has no scripts. I have two buttons with click detectors. I have a screen which contains the two main scripts, the Timer Script which manages the visual timer and the pause-go buttons, and the Bleep Script which manages the sounds. The Timer Script works perfectly well alone, just has no sound. If you would like me to show you what the Timer Script is, just ask. For now I will assume the problem is purely in the Bleep Script. I will show the code used to detect how many beeps should be played per second as this is where I think the problem is:
z = script.Parent.TimeLeft.Value --This is finding the time left in the bomb's timer. function BleepSpeed() if z >= 25 then bleepTimes = 1 elseif z == 24 or 23 or 22 or 21 or 20 or 19 or 18 or 17 or 16 or 15 then bleepTimes = 2 elseif z == 14 or 13 or 12 or 10 then bleepTimes = 3 elseif z == 9 or 8 or 7 or 6 or 5 then bleepTimes = 4 elseif z == 4 or 3 or 2 or 1 or 0 then bleepTimes = 5 else bleepTimes = 1 end end
the bleepTimes variable is used in extremely primitive code to play the sound a certain amoount of times. If bleepTimes = 1 then it would do one bleep per second.
This next bit of code is the loop that I use to keep ticking to the next second. Both scripts have a similar bit of code, just that one changes the visual timer while this one plays the sound.
z = script.Parent.Time.Value -This is setting the Time Left in the timer to full at the start. keepItGoing = true --This keeps the loop checking forever. while keepItGoing == true do wait(0.025) --Meant to reduce lag if Triggered == true then --If the bomb is on, not paused if script.Parent.CriticalError.Value == false then --[[If the bomb is not experiencing an Error, which is a non-relevant part of my bomb's code]]-- CheckBleeps() --Calls the function for checking, and then playing the sounds. wait(1 - y) --Waits one second minus the time taken to play the sounds. end end end
And finally, the CheckBleeps function, which is played every second the bomb is activated.
y = 0 --This is at the start of the game, not the function, but it is relevant to this code. function CheckBleeps() z = z - 1 BleepSpeed() print(z) if bleepTimes == 1 then script.Parent.Parent.Parent.Parent.Bleep:Play() y = 0 elseif bleepTimes == 2 then script.Parent.Parent.Parent.Parent.Bleep:Play() wait(0.2) script.Parent.Parent.Parent.Parent.Bleep:Play() y = 0.2 elseif bleepTimes == 3 then script.Parent.Parent.Parent.Parent.Bleep:Play() wait(0.2) script.Parent.Parent.Parent.Parent.Bleep:Play() wait(0.2) script.Parent.Parent.Parent.Parent.Bleep:Play() y = 0.4 elseif bleepTimes == 4 then script.Parent.Parent.Parent.Parent.Bleep:Play() wait(0.2) script.Parent.Parent.Parent.Parent.Bleep:Play() wait(0.2) script.Parent.Parent.Parent.Parent.Bleep:Play() wait(0.2) script.Parent.Parent.Parent.Parent.Bleep:Play() y = 0.6 elseif bleepTimes == 5 then script.Parent.Parent.Parent.Parent.Bleep:Play() wait(0.2) script.Parent.Parent.Parent.Parent.Bleep:Play() wait(0.2) script.Parent.Parent.Parent.Parent.Bleep:Play() wait(0.2) script.Parent.Parent.Parent.Parent.Bleep:Play() wait(0.2) script.Parent.Parent.Parent.Parent.Bleep:Play() y = 0.8 end end
If there is anything more that I need to add or any information you lack, or even improper fomatting for this site, please tell me. I will be checking this often.
Thank you so much for your time, even if you're just reading this :D
When finding your bleepTimes
, you are using or
incorrectly.
I am referencing the lines where you make checks like these:
elseif z == 4 or 3 or 2 or 1 or 0 then
You are first checking if z is 4, which is valid. Then, when you say or 3
, you are checking if 3 exists, not if z is 3. To do this, you should say or z == 3
.
However, this is also very inefficient and can be tedious. Instead, to check if z is in a specified range, use the operations >=, >, <=, <
. The statement above can be rewritten as:
else if z <= 4 and z >= 0 then -- checks if z is between 4 and 0 (inclusive)
Hope this helps.
EDIT:
In addition to the above, I highly recommend making sure your code is clean and you are using optimal keywords.
One example I notice right off the bat is where you've written this:
while keepItGoing == true do
Instead of checking if something is true or false using == true
, you can simply say:
while keepItGoing do -- checks if keepItGoing is true if not script.Parent.CriticalError.Value then -- checks if CriticalError's value is false -- i would reference it in a variable also so: local criticalError = script.Parent.CriticalError if not criticalError.Value then -- checks if CriticalError's value is false
Finally, be sure to use loops rather than rewriting the same lines over and over. With this, I am referencing where you say:
elseif bleepTimes == 5 then script.Parent.Parent.Parent.Parent.Bleep:Play() wait(0.2) script.Parent.Parent.Parent.Parent.Bleep:Play() wait(0.2) script.Parent.Parent.Parent.Parent.Bleep:Play() wait(0.2) script.Parent.Parent.Parent.Parent.Bleep:Play() wait(0.2) script.Parent.Parent.Parent.Parent.Bleep:Play() y = 0.8 end
You can instead use a for
loop to do the exact same thing, but much less work and much cleaner:
local bleepSound = script.Parent.Parent.Parent.Parent.Bleep for i=1, bleepTimes do bleepSound:Play() wait(0.2) end y = (bleepTimes - 1) * 0.2 -- if bleepTimes is 1, y = 0, bleepTimes is 2, y = 0.2, etc..
Good luck!