Hi
This is my first attempt at scripting, but I'm have issues getting this to work.
Basically, I want the player to answer some questions where they get 10 points for a right answer and -10 for a wrong answer. But I just can't get it to work, because scripting isn't my thing and like I said this is my first attempt.
See Script Below.
-- Quiz Script
-- Initialize score
score = 0
-- Questions and Answers
questions = {
{
question = "What is the capital of France?",
answers = {
"Berlin",
"London",
"Paris" -- Correct answer
}
},
{
question = "Which planet is known as the Red Planet?",
answers = {
"Earth",
"Mars", -- Correct answer
"Jupiter"
}
},
-- Add more questions as needed
}
-- Current question index
currentQuestionIndex = 1
-- Function to display the current question and answers
function DisplayQuestion()
-- Display the question
Print("Question: " .. questions[currentQuestionIndex].question)
-- Display answer options
for i, answer in ipairs(questions[currentQuestionIndex].answers) do
Print(i .. ". " .. answer)
end
end
-- Function to check the player's answer
function CheckAnswer(selectedAnswer)
local correctAnswerIndex = 0
-- Find the index of the correct answer
for i, answer in ipairs(questions[currentQuestionIndex].answers) do
if answer == questions[currentQuestionIndex].answers[1] then
correctAnswerIndex = i
end
end
-- Check if the selected answer is correct
if selectedAnswer == correctAnswerIndex then
Print("Correct!")
score = score + 10
else
Print("Wrong!")
score = score - 10
end
-- Move to the next question
currentQuestionIndex = currentQuestionIndex + 1
-- Check if there are more questions
if currentQuestionIndex <= #questions then
DisplayQuestion()
else
Print("Quiz completed! Your final score is: " .. score)
end
end
-- Display the first question when the game starts
DisplayQuestion()
Processor i9-9900k CPU@ 3.60GHZ
Ram 32GB
Windows 10 64bit
GeForce RTX 2060