I want to make a function that can be called and returned as a bool, true or false, based on if the player did something or did not do something in a game.
I’ve included some scripts below of what I’ve been trying, but it just gets stuck in the wait loop. From what I’ve researched it should yield until the function completes, but that just didn’t happen at all for me.
I’ve looked at a post about the calling “return” in scripts and based on it im using returns atleast somewhat correctly. For all i know though that post couldve been not in-depth enough. Do I have a major misunderstanding of returns? please let me know! I’ve also tested the MultipleChoiceType, which i believe is correct because whenever i call it and put prints next to the returns it prints them correctly so it probably isnt that.
local function MultipleChoiceType(Question, AnswerList)
--Answer list is a table with 4 answers, the first of which being correct.
QuestionText.Text = Question
MultipleChoiceFrame.Visible = true
local ChosenList = {}
for _, answerchoice in MultipleChoiceFrame:GetChildren() do
local answerchosen
repeat
answerchosen = math.random(1,4)
until not table.find(ChosenList, answerchosen)
table.insert(ChosenList, answerchosen)
answerchoice.Text = AnswerList[answerchosen]
answerchoice.MouseButton1Click:Connect(function()
if answerchosen == 1 then
return true
else
return false
end
end)
end
end
repeat
local IsCorrect = MultipleChoiceType("question", {"answer1", "answer2", "answer3", "Answer4"})
repeat
print("idk yet")
wait()
until IsCorrect ~= nil
print("IK")
CheckCorrect(IsCorrect)
until IsCorrect == true
Please help ive been stuck on this for like 1-2 hours ):
You are returning inside a anonimous function
You can yield it if you want:
local function MultipleChoiceType(Question, AnswerList)
--Answer list is a table with 4 answers, the first of which being correct.
QuestionText.Text = Question
MultipleChoiceFrame.Visible = true
local ChosenList = {}
for _, answerchoice in MultipleChoiceFrame:GetChildren() do
local answerchosen
repeat
answerchosen = math.random(1,4)
until not table.find(ChosenList, answerchosen)
table.insert(ChosenList, answerchosen)
answerchoice.Text = AnswerList[answerchosen]
answerchoice.MouseButton1Click:Wait()
if answerchosen == 1 then
return true
else
return false
end
end
end
A procedural approach like this is really ill-suited. You should just have every button fire a single function + the number of the button and then check if the answer is correct there.
local answer
local function checkAnswer(num)
if num == answer then print("Win!") end -- do your win condition here
end
local buttons = MultipleChoiceFrame:GetChildren()
for i, choice in buttons do
choice.MouseButton1Click:Connect(function()
checkAnswer(i)
end)
end
local function MultipleChoiceType(Question, AnswerList)
-- ... setup omitted
local map = {}
for i, choice in buttons do
-- get a random answer
local answerchoice = math.random(1, 4)
-- get the next free spot from the random
-- this algo is less random but avoids infinite loops
while map[answerchoice] do
-- increment loop from 1-4
answerchoice = ((answerchoice + 1) % 4) + 1
end
map[answerchoice] = i
choice.Text = AnswerList[answerchoice]
end
-- store the button with the correct answer
answer = map[1]
end
The algo at the end might be wrong idk I’m tired but you should get the idea. Map answer->button instead of using a list.