How could I run this line of code when I want to?

Hi, I am trying to make a model where the user has to answer a question about the word they saw appear. The only issue is that this error’s when I try to run it. I’m not sure how I could prevent this line from running until needed. I thought about wrapping it as a string but otherwise I would have to run it with loadstring but this requires LoadStringEnabled to be toggled on which I don’t want, and that I also don’t wanna keep running math.random with every function having it’s own question. I wanna wrap all my questions and answers in a table.

local button = script.Parent:WaitForChild("Button").ClickDetector
local text = script.Parent:WaitForChild("Screen").SurfaceGui.TextLabel
local http = game:GetService("HttpService")
local website = "https://random-word-api.herokuapp.com/word"
local playing = false
local userplaying : Player = nil
local difficulty = 1
local word = nil
local score = 0

local uhh = {[2] = "nd", [3] = "rd"}

local questions = { --ISSUE STARTS HERE (Notice the "Answer" variable having a premade string.sub to run when I want to.)
	{Question = "What is the first letter in the word?",Answer = string.sub(word,1,1),Diff = 1},
	{Question = "What is the last letter in the word?",Answer = string.sub(word,#word,#word),Diff = 1},
	{Question = string.format("What is the %s letter in the word?",uhh[math.random(2,#word-1)]),Answer = string.sub(word,1,1),Diff = 1}
} --ISSUE ENDS HERE
local selectedquestion = nil
local chatted = nil

How would I do this?

You could just set the question to something once it is asked

That’s (I think?) exactly what I did here as a function.

local function AskQuestion()
	text.Text = "Your word is..."
	task.wait(2)
	word = http:JSONDecode(http:GetAsync("https://random-word-api.herokuapp.com/word"))[1]
	text.Text = word
	task.wait(5)
	text.Text = "..."
	task.wait(1.5)
	selectedquestion = questions[math.random(#questions)] --variable chooses a random question
	print(selectedquestion.Answer)
	text.Text = selectedquestion.Question
	chatted = userplaying.Chatted:Connect(function(message)
		print("passed")
		if message == selectedquestion.Answer then
			chatted:Disconnect()
			text.Text = "Correct!"
			task.wait(3)
			AskQuestion()
		else
			chatted:Disconnect()
			GameOver()
		end
	end)
end

Figured it out. Just had to move the table somewhere else for it to work!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.