Advertisement
KingAesthetic

Luau Example 3

Feb 2nd, 2025
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.32 KB | None | 0 0
  1. local wordsToType = 30 -- Number of words to type before level up
  2. local currentWordIndex = 1
  3. local points = 0
  4. local multiplier = 1
  5. local timeElapsed = 0
  6. local timer = 5
  7. local gameRunning = false
  8. local typedWord = ""
  9. local currentLevelNumber = 1
  10. local usedWords = {} -- To Keep track of used/typed words
  11. local correctWordsCount = 0 -- counter for correctly typed words
  12.  
  13. -- function for handling word submission
  14. local function handleWordSubmission()
  15.     if not gameRunning then return end
  16.  
  17.     local typedWord = TextBox.Text
  18.     local wordToType = currentWord.Text
  19.  
  20.     if typedWord:lower() == wordToType:lower() then
  21.         -- Word is correct
  22.         points = points + 1 * multiplier
  23.         pointText.Text = tostring(points)
  24.         timer = timer + 2 -- Add 2 seconds to the timer
  25.         TextBox.Text = "" -- Clear the TextBox
  26.  
  27.         -- Update words Left
  28.         wordsToType = wordsToType - 1
  29.         wordsLeft.Text = wordsToType .. " Words left until level " .. (currentLevelNumber + 1)
  30.        
  31.         -- Add the word to the usedWords Table
  32.         table.insert(usedWords, wordToType)
  33.        
  34.         -- increment the count of correct words
  35.         correctWordsCount = correctWordsCount + 1
  36.        
  37.         -- check if it's time to increase the multiplier
  38.         if correctWordsCount % 13 == 0 then
  39.             multiplier = multiplier + 1
  40.             multiplierText.Text = "x" .. tostring(multiplier)
  41.         end
  42.        
  43.         -- Check if all words have been typed
  44.         if wordsToType == 0 then
  45.             -- Logic for leveling up/game continuation
  46.             print("Level Up!")
  47.             currentLevelNumber = currentLevelNumber + 1
  48.             currentLevel.Text = "Level " .. currentLevelNumber
  49.  
  50.             if currentLevelNumber == 2 then
  51.                 wordsToType = 30 -- Reset for Level 2
  52.                 wordList = level2WordList -- Switch to level 2 words
  53.                 usedWords = {}
  54.                 correctWordsCount = 0 -- Reset the counter for the correct words
  55.                 wordsLeft.Text = wordsToType .. " Words left until Level " .. (currentLevelNumber + 1)
  56.                
  57.                
  58.             elseif currentLevelNumber == 3 then
  59.                 wordsToType = 30 -- Reset for Level 3
  60.                 wordList = level3WordList -- Switch to level 3 words
  61.                 usedWords = {}
  62.                 correctWordsCount = 0
  63.                 wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
  64.                
  65.                
  66.             elseif currentLevelNumber == 4 then
  67.                 wordsToType = 30 -- Reset for Level 4
  68.                 wordList = level4WordList -- Switch to Level 4 words
  69.                 usedWords = {}
  70.                 correctWordsCount = 0
  71.                 wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
  72.                
  73.                
  74.             elseif currentLevelNumber == 5 then
  75.                 wordsToType = 30 -- Reset for Level 5
  76.                 wordList = level5WordList -- Switch to Level 5 words
  77.                 usedWords = {}
  78.                 correctWordsCount = 0
  79.                 wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
  80.                
  81.             elseif currentLevelNumber == 6 then
  82.                 wordsToType = 30 -- Reset for Level 6
  83.                 wordList = level6WordList -- Switch to Level 6 words
  84.                 usedWords = {}
  85.                 correctWordsCount = 0
  86.                 wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
  87.            
  88.             elseif currentLevelNumber == 7 then
  89.                 wordsToType = 30 -- Reset for Level 7
  90.                 wordList = level7WordList -- Switch to Level 7 words
  91.                 usedWords = {}
  92.                 correctWordsCount = 0
  93.                 wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
  94.            
  95.             elseif currentLevelNumber == 8 then
  96.                 wordsToType = 30 -- Reset for Level 8
  97.                 wordList = level8WordList -- Switch to Level 8 words
  98.                 usedWords = {}
  99.                 correctWordsCount = 0
  100.                 wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
  101.            
  102.             elseif currentLevelNumber == 9 then
  103.                 wordsToType = 30 -- Reset for Level 9
  104.                 wordList = level9WordList -- Switch to Level 9 Words
  105.                 usedWords = {}
  106.                 correctWordsCount = 0
  107.                 wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
  108.            
  109.             elseif currentLevelNumber == 10 then
  110.                 wordsToType = 30 -- Reset for Level 10
  111.                 wordList = level10WordList --  Switch to Level 10 words
  112.                 usedWords = {}
  113.                 correctWordsCount = 0
  114.                 wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
  115.             end
  116.         end
  117.  
  118.         -- Select and display the next word
  119.         local availableWords = {}
  120.         for _, word in ipairs(wordList) do
  121.             if not table.find(usedWords, word) then
  122.                 table.insert(availableWords, word)
  123.             end
  124.         end
  125.        
  126.         if #availableWords > 0 then
  127.             -- There are still words left to type that haven't been used
  128.             local randomIndex = math.random(1, #availableWords)
  129.             currentWord.Text = availableWords[randomIndex]
  130.         else
  131.             -- incase all words has been used, reset the usedWords Table
  132.             print("All words have been used, resetting usedWords.")
  133.             usedWords = {}
  134.             local randomIndex = math.random(1, #wordList)
  135.             currentWord.Text = wordList[randomIndex]
  136.         end
  137.     else
  138.         -- word is incorrect , clear Textbox for retry
  139.         TextBox.Text = ""
  140.     end
  141. end
  142.  
  143.  
  144. -- Function to start the game mechanics
  145. local function StartGameMechanics()
  146.     -- Make UI elements visible
  147.     currentLevel.Visible = true
  148.     currentWord.Visible = true
  149.     wordsLeft.Visible = true
  150.     timerText.Visible = true
  151.     timeElapsedText.Visible = true
  152.     pointText.Visible = true
  153.     gameOverText.Visible = false
  154.     TextBox.Visible = true -- Make TextBox Visible
  155.  
  156.     -- Set initial values
  157.     currentLevel.Text = "Level 1"
  158.     wordsLeft.Text = wordsToType .. " words left until Level 2"
  159.     pointText.Text = tostring(points)
  160.     multiplierText.Text = "x" .. tostring(multiplier)
  161.     timerText.Text = tostring(timer)
  162.     timeElapsedText.Text = "Time Elapsed: " .. tostring(timeElapsed) .. "s"
  163.     TextBox.Text = "" -- Text Box should be empty at the start
  164.     usedWords = {} -- Clear used words at the start of the game
  165.     correctWordsCount = 0 -- counter initialized for correct words
  166.    
  167.     -- Select and display the first word
  168.     currentWord.Text = wordList[math.random(1, #wordList)]
  169.     gameRunning = true
  170.    
  171.     -- connect TextBox functionality
  172.     TextBox.FocusLost:Connect(function(enterPressed)
  173.         if enterPressed then
  174.             handleWordSubmission()
  175.         end
  176.     end)
  177.    
  178.     -- Start the timer
  179.     while gameRunning do
  180.         wait(1)
  181.         if timer > 0 then
  182.             timer = timer - 1
  183.             timerText.Text = tostring(timer)
  184.             timeElapsed = timeElapsed + 1
  185.             timeElapsedText.Text = "Time Elapsed: " .. tostring(timeElapsed) .. "s"
  186.         else
  187.             -- Game Over Logic
  188.             gameRunning = false
  189.             currentLevel.Visible = false
  190.             currentWord.Visible = false
  191.             wordsLeft.Visible = false
  192.             TextBox.Visible = false
  193.  
  194.             -- Keep Point, Multiplier, Timer, TimeElapsed, and show GameOverText
  195.             pointText.Visible = true
  196.             multiplierText.Visible = true
  197.             timerText.Visible = true
  198.             timeElapsedText.Visible = true
  199.             gameOverText.Visible = true
  200.  
  201.             print("Game Over")
  202.         end
  203.     end
  204. end
  205.  
  206. -- Connect to the StartGame button
  207. local startGameButton = screenGUI:WaitForChild("StartGame", 5)
  208. if not startGameButton then
  209.     warn("StartGame button not found within 5 seconds")
  210.     return
  211. end
  212.  
  213. startGameButton.MouseButton1Click:Connect(function()
  214.     StartGameMechanics()
  215. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement