Could I do this better? (Countdown/loop, if statement and Random)

Hello!

As of now I am making a FNAF type game. I decided now to when the night (game) is almost over and the player is about to win, I decided to add ticking sounds (10 seconds before winning) as to “hype” up the player of winning.
I decided to add 2 sounds to make it random. One will be picked
And if you look at the code below you can probably see some improvements.

Basically I am able to do a function of picking random tables, but I am not able to do the same for objects.
And I really feel that the (both) “if” statements could be replaced with something more simple.


local Tick = --Sound
local Tick2 = --Sound2
local RandomTick = math.random(1,2)
print(RandomTick)

--[...]
local timeRemaining6 = 50

while timeRemaining6 > 0 do
--For all other hours (12AM - 6Am), this is the same without the 'if' statement.
	print("Seconds remaining: " .. timeRemaining6)
	wait(1)
	timeRemaining6 = timeRemaining6 - 1
--Basically when there's 10 seconds remaining to the night I decided to add a Ticking sound.
--I feel this could be done better, as in waste less memory.

	if timeRemaining6 == 10 then
--Decides which sound to play, I feel this would be better. 
--Normally I'm able to pick random tables but I don't think it can work with this.

		if RandomTick == 1 then
			Tick:Play()
		else if RandomTick == 2 then
				Tick2:Play()
			end
		end
	end
end
--When timeRemaining6 is finished it goes over the next lines of code.

print("Timer reached zero! The player won!")
Tick:Pause()
Tick2:Pause()

So as you read the code there’s some improvements to be made (at least based on what I think).

I’d like to see how:

  • I can pick random objects and execute functions with them.
  • Have a more efficient way (in the loop) of when there’s a specific value to do a function.

Thanks for reading!

I just figured out I can add objects to tables, I’ll try it right now.

rng = Random.new(tick())
-- put this in your loop so it plays a random new sound every time
local thisIsALoop do

	local pick = rng:NextInteger(1,2)

	if RandomTick == 1 then
		Tick:Play()
	else
		Tick2:Play()
	end

end
1 Like

Thanks for the help.

However, I need either one to play (10 seconds before the end). Only one will play. I could try some of the things you suggested.

1 Like

There’s nothing wrong with the way you implemented it. It’s very straightforward, so there’s no need to complicate things. But, if you want to put things into tables, it could help with reuse if you change things later.

btw, it kind of looks like there’s an extra “end” statement after the “Tick2:Play()” line.

EDIT: Nvm I see you split “elseif” into “else if” so you basically have a nested if-statement going on.

Basically I have (like mentioned before) figured out I can insert objects into tables and pick random. But I cannot do functions.

Code:

local RandomTick = {Tick, Tick2}

local value = Random.new(1,#RandomTick)


local picked_Sound = RandomTick[value]

print(tostring(picked_Sound))

picked_Sound:Play()
-- ^This is where there's a problem. ^

@WhoooDattt @Thanks4DaLimbs
So as of now I am trying to do this:

local RandomTick = {Tick, Tick2}

local value = Random.new(1,#RandomTick)


local picked_Sound = RandomTick[value]

print(tostring(picked_Sound))
--When printing this it returns with nil so I am probably doing something wrong here.

picked_Sound:Play()
-- ^This is where there's a problem. ^

Basically I can randomly choose the objects but cannot do any functions. I think I am doing something wrong with how I am picking the random objects as it returns nil.

I just figured it out:

local RandomTick = {Tick, Tick2}

local value = math.random(1,#RandomTick) 

local picked_value = RandomTick[value]

print(tostring(picked_value))

picked_value:Play()

Random.new makes a new RNG. the argument is the seed. Do:

Random.new(tick()):NextInteger(1,#RandomTick)
1 Like