Trouble with making fnaf like minigame

Hello. I am trying to make a minigame similar to one there is is FNAF. A little guy is switching positions and you can only hear him when he moves. You can not see. To see you have to use your flashlight and catch him before he gets too close to you. If he gets too close you die. But I am having some trouble while making it. It works fine if I click my flashlight for every time I hear a sound. But if I double click my flashlight it gets stuck. I know the system I have made is terrible but it was after I decided I did not want to randomize where the guy moves, so instead he should move accordingly to the points I have made. First point 1 and then up to point 5. When he reaches point 5 which is directly in front of the player he should start over at point 1 again and so on…

function move(location)
	ongoing = true
	
	local randomNumber = math.random(1,3)
	if randomNumber == 1 then
		workspace.Audio.LucentStep1:Play()
	elseif randomNumber == 2 then
		workspace.Audio.LucentStep2:Play()
	elseif randomNumber == 3 then
		workspace.Audio.LucentLaugh:Play()
	end
	
	if location < 5 and flashlight.Enabled == false and ongoing == true then
		for i,v in pairs(workspace:GetChildren()) do
			if v:IsA("Model") and v.Name == "PlushLucent_"..tostring(currentLocation) then
				v:Destroy()
			end
		end			
		
		currentLocation = location
		
		local plush = plushes["PlushLucent_"..location]:Clone()
		plush.Parent = workspace
		
		ongoing = false
	elseif location == 5 and flashlight.Enabled == false and ongoing == true then
		for i,v in pairs(workspace:GetChildren()) do
			if v:IsA("Model") and v.Name == "PlushLucent_"..tostring(currentLocation) then
				v:Destroy()
			end
		end
		
		currentLocation = location
		
		local plush = plushes["PlushLucent_"..location]:Clone()
		plush.Parent = workspace
		
		task.wait(3)
		
		if flashlight.Enabled == false and ongoing == true then
			player.PlayerGui.Gameplay.LucentTimer.Visible = false
			player.PlayerGui.Gameplay.LucentTimer.Text = "2:00"

			currentCamera.CFrame = jumpscareCam.CFrame

			local anim = plushJumpscareModel.Animation
			local humanoid = plushJumpscareModel:WaitForChild("Humanoid")
			local animTrack = humanoid:LoadAnimation(anim)

			animTrack:Play()
			player.PlayerScripts.Movement.Sound:Play()

			dead = true
		elseif flashlight.Enabled == true and ongoing == true then
			count1 = false
			count2 = false
			count3 = false
			count4 = false
			count5 = false
			
			currentLocation = 0

			ongoing = false
		end
	end
end
	minigames.Buttons.LucentPlush.MouseButton1Click:Connect(function()
		if not minigame then
			default()
			
			tween:play(main.Parent.Fade, "DefaultLong", { BackgroundTransparency = 0 }, true)
			main.Parent:Destroy()
			
			workspace.Audio.Menu:Stop()
			workspace.Audio.Ambient:Play()
			player.PlayerGui:WaitForChild("Gameplay").Handler.Disabled = true
			changeCamera()
			
			player.PlayerGui:WaitForChild("Gameplay").LucentTimer.Visible = true
			player.PlayerGui:WaitForChild("Gameplay").LucentTimer.Text = "2:00"
			task.wait(1)
			
			local timer = player.PlayerGui.Gameplay.LucentTimer
			
			coroutine.wrap(function()
				repeat
					if seconds <= 0 then
						minutes = minutes - 1
						seconds = 59
					else
						seconds = seconds - 1
					end
					if seconds <= 9  then
						timer.Text = tostring(minutes)..":0"..tostring(seconds)
					else
						timer.Text = tostring(minutes)..":"..tostring(seconds)
					end
					wait(1)
				until minutes <= 0 and seconds <= 0
			end)()
			
			UserInputService.InputBegan:Connect(function(input)
				if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.MouseButton2 then
					flashlight.Enabled = true
					workspace.Audio.Light:Play()
				end
			end)

			UserInputService.InputEnded:Connect(function(input)
				if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.MouseButton2 then
					flashlight.Enabled = false
					workspace.Audio.Light:Play()
				end
			end)
			
			while dead == false and win == false and flashlight.Enabled == false and ongoing == false do
				wait(math.random(2,6))
				if count1 == false and count2 == false and count3 == false and count4 == false and count5 == false and flashlight.Enabled == false and currentLocation == 0 then
					count1 = true
					ongoing = true
					move(1)
				elseif count2 == false and count1 == true and count3 == false and count4 == false and count5 == false and flashlight.Enabled == false and currentLocation == 1 then
					count2 = true
					ongoing = true
					move(2)
				elseif count3 == false and count1 == true and count2 == true and count4 == false and count5 == false and flashlight.Enabled == false and currentLocation == 2 then
					count3 = true
					ongoing = true
					move(3)
				elseif count4 == false and count1 == true and count3 == true and count2 == true and count5 == false and flashlight.Enabled == false and currentLocation == 3 then
					count4 = true
					ongoing = true
					move(4)
				elseif count5 == false and count1 == true and count3 == true and count2 == true and count4 == true and flashlight.Enabled == false and currentLocation == 4 then
					count5 = true
					ongoing = true
					move(5)
				end
			end
		end
	end)
end

I fixed it by making this:

			repeat wait()
				if flashlight.Enabled == false then
					for i = 1,5 do 
						wait(math.random(10,15))

						if first == false and i == 1 then
							first = true
							move(1)
						elseif second == false and i == 2 then
							second = true
							move(2)
						elseif third == false and i == 3 then
							third = true
							move(3)
						elseif fourth == false and i == 4 then
							fourth = true
							move(4)
						elseif fifth == false and i == 5 then
							fifth = true
							move(5)
						end
					end					
				end
			until dead == true or win == true
1 Like