Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local wordsToType = 30 -- Number of words to type before level up
- local currentWordIndex = 1
- local points = 0
- local multiplier = 1
- local timeElapsed = 0
- local timer = 5
- local gameRunning = false
- local typedWord = ""
- local currentLevelNumber = 1
- local usedWords = {} -- To Keep track of used/typed words
- local correctWordsCount = 0 -- counter for correctly typed words
- -- function for handling word submission
- local function handleWordSubmission()
- if not gameRunning then return end
- local typedWord = TextBox.Text
- local wordToType = currentWord.Text
- if typedWord:lower() == wordToType:lower() then
- -- Word is correct
- points = points + 1 * multiplier
- pointText.Text = tostring(points)
- timer = timer + 2 -- Add 2 seconds to the timer
- TextBox.Text = "" -- Clear the TextBox
- -- Update words Left
- wordsToType = wordsToType - 1
- wordsLeft.Text = wordsToType .. " Words left until level " .. (currentLevelNumber + 1)
- -- Add the word to the usedWords Table
- table.insert(usedWords, wordToType)
- -- increment the count of correct words
- correctWordsCount = correctWordsCount + 1
- -- check if it's time to increase the multiplier
- if correctWordsCount % 13 == 0 then
- multiplier = multiplier + 1
- multiplierText.Text = "x" .. tostring(multiplier)
- end
- -- Check if all words have been typed
- if wordsToType == 0 then
- -- Logic for leveling up/game continuation
- print("Level Up!")
- currentLevelNumber = currentLevelNumber + 1
- currentLevel.Text = "Level " .. currentLevelNumber
- if currentLevelNumber == 2 then
- wordsToType = 30 -- Reset for Level 2
- wordList = level2WordList -- Switch to level 2 words
- usedWords = {}
- correctWordsCount = 0 -- Reset the counter for the correct words
- wordsLeft.Text = wordsToType .. " Words left until Level " .. (currentLevelNumber + 1)
- elseif currentLevelNumber == 3 then
- wordsToType = 30 -- Reset for Level 3
- wordList = level3WordList -- Switch to level 3 words
- usedWords = {}
- correctWordsCount = 0
- wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
- elseif currentLevelNumber == 4 then
- wordsToType = 30 -- Reset for Level 4
- wordList = level4WordList -- Switch to Level 4 words
- usedWords = {}
- correctWordsCount = 0
- wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
- elseif currentLevelNumber == 5 then
- wordsToType = 30 -- Reset for Level 5
- wordList = level5WordList -- Switch to Level 5 words
- usedWords = {}
- correctWordsCount = 0
- wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
- elseif currentLevelNumber == 6 then
- wordsToType = 30 -- Reset for Level 6
- wordList = level6WordList -- Switch to Level 6 words
- usedWords = {}
- correctWordsCount = 0
- wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
- elseif currentLevelNumber == 7 then
- wordsToType = 30 -- Reset for Level 7
- wordList = level7WordList -- Switch to Level 7 words
- usedWords = {}
- correctWordsCount = 0
- wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
- elseif currentLevelNumber == 8 then
- wordsToType = 30 -- Reset for Level 8
- wordList = level8WordList -- Switch to Level 8 words
- usedWords = {}
- correctWordsCount = 0
- wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
- elseif currentLevelNumber == 9 then
- wordsToType = 30 -- Reset for Level 9
- wordList = level9WordList -- Switch to Level 9 Words
- usedWords = {}
- correctWordsCount = 0
- wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
- elseif currentLevelNumber == 10 then
- wordsToType = 30 -- Reset for Level 10
- wordList = level10WordList -- Switch to Level 10 words
- usedWords = {}
- correctWordsCount = 0
- wordsLeft.Text = wordsToType .. "Words left until Level " .. (currentLevelNumber + 1)
- end
- end
- -- Select and display the next word
- local availableWords = {}
- for _, word in ipairs(wordList) do
- if not table.find(usedWords, word) then
- table.insert(availableWords, word)
- end
- end
- if #availableWords > 0 then
- -- There are still words left to type that haven't been used
- local randomIndex = math.random(1, #availableWords)
- currentWord.Text = availableWords[randomIndex]
- else
- -- incase all words has been used, reset the usedWords Table
- print("All words have been used, resetting usedWords.")
- usedWords = {}
- local randomIndex = math.random(1, #wordList)
- currentWord.Text = wordList[randomIndex]
- end
- else
- -- word is incorrect , clear Textbox for retry
- TextBox.Text = ""
- end
- end
- -- Function to start the game mechanics
- local function StartGameMechanics()
- -- Make UI elements visible
- currentLevel.Visible = true
- currentWord.Visible = true
- wordsLeft.Visible = true
- timerText.Visible = true
- timeElapsedText.Visible = true
- pointText.Visible = true
- gameOverText.Visible = false
- TextBox.Visible = true -- Make TextBox Visible
- -- Set initial values
- currentLevel.Text = "Level 1"
- wordsLeft.Text = wordsToType .. " words left until Level 2"
- pointText.Text = tostring(points)
- multiplierText.Text = "x" .. tostring(multiplier)
- timerText.Text = tostring(timer)
- timeElapsedText.Text = "Time Elapsed: " .. tostring(timeElapsed) .. "s"
- TextBox.Text = "" -- Text Box should be empty at the start
- usedWords = {} -- Clear used words at the start of the game
- correctWordsCount = 0 -- counter initialized for correct words
- -- Select and display the first word
- currentWord.Text = wordList[math.random(1, #wordList)]
- gameRunning = true
- -- connect TextBox functionality
- TextBox.FocusLost:Connect(function(enterPressed)
- if enterPressed then
- handleWordSubmission()
- end
- end)
- -- Start the timer
- while gameRunning do
- wait(1)
- if timer > 0 then
- timer = timer - 1
- timerText.Text = tostring(timer)
- timeElapsed = timeElapsed + 1
- timeElapsedText.Text = "Time Elapsed: " .. tostring(timeElapsed) .. "s"
- else
- -- Game Over Logic
- gameRunning = false
- currentLevel.Visible = false
- currentWord.Visible = false
- wordsLeft.Visible = false
- TextBox.Visible = false
- -- Keep Point, Multiplier, Timer, TimeElapsed, and show GameOverText
- pointText.Visible = true
- multiplierText.Visible = true
- timerText.Visible = true
- timeElapsedText.Visible = true
- gameOverText.Visible = true
- print("Game Over")
- end
- end
- end
- -- Connect to the StartGame button
- local startGameButton = screenGUI:WaitForChild("StartGame", 5)
- if not startGameButton then
- warn("StartGame button not found within 5 seconds")
- return
- end
- startGameButton.MouseButton1Click:Connect(function()
- StartGameMechanics()
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement