Does this go into starter gui? Also I didn’t define startTest
local startTest = (what do I define it as?)
Does this go into starter gui? Also I didn’t define startTest
local startTest = (what do I define it as?)
I’m keeping everything under the scope of one script, so same script as before. When I accessed startTest I did it with (), so that means startTest should be a function. And by its name, it purpose should be to start a new test. Since thats what you want to happen when a part of a certain name is touched. I outlined what a function for that would look like above to.
Start test is underlined in blue, also I’m not sure if I’m supposed to add it at the very top or bottom. Nothing comes out in the output, but it doesn’t work.
local players=game:GetService('Players')
local player=players.LocalPlayer
player.CharacterAdded:Connect(function (character)
local humanoid=character:WaitForChild('Humanoid')
humanoid.Touched:Connect(function (part)
if part.Name=='CBBrick' then
startTest()
end
end)
end)
local questions = {
{"Red",{"Red","Orange","Yellow","Green","Blue","Purple"},1},
{"Orange",{"Red","Orange","Yellow","Green","Blue","Purple"},2},
{"Yellow",{"Red","Orange","Yellow","Green","Blue","Purple"},3},
{"Green",{"Red","Orange","Yellow","Green","Blue","Purple"},4},
{"Blue",{"Red","Orange","Yellow","Green","Blue","Purple"},5},
{"Purple",{"Red","Orange","Yellow","Green","Blue","Purple"},6}
}
local numberCorrect = 0
local NumberOfQuestions = 6
local function finish()
local percentCorrect = math.floor(numberCorrect/NumberOfQuestions * 100)
--DISABLES THE GUI
game.Players.LocalPlayer.PlayerGui.ColorBlindLockScreenTest.Frame.Visible = false
game.Players.LocalPlayer.PlayerGui.ColorBlindLockScreenTest.TestEnd.Visible = true
game.Players.LocalPlayer.PlayerGui.ColorBlindLockScreenTest.TestEnd.Number.Text = percentCorrect
end
local Question, Answers, CorrectAnswer
local function changeQuestion()
-- load question
local TempQuestionNumber = math.random(1,#questions)
local QuestionData = questions[TempQuestionNumber]
Question, Answers, CorrectAnswer = unpack(QuestionData)
-- Set all frames visibility to false
local UI_objs=script.Parent.Frame:GetChildren()
for i,obj in pairs(UI_objs) do
if (obj:IsA('Frame')) then
obj.Visible=false
end
end
-- Set the corresponding frame's visibility to true if it exists
if script.Parent.Frame:FindFirstChild(Answers[CorrectAnswer]) then
script.Parent.Frame:FindFirstChild(Answers[CorrectAnswer]).Visible=true
end
table.remove(questions,TempQuestionNumber)
end
local function answer(name)
if string.match(name, Answers[CorrectAnswer]) then -- If button name contains answer than mark correct; works within this specific context, may not work for another
numberCorrect = numberCorrect+1
end
if #questions<=0 then -- If all questions are taken then finish test
finish()
else
changeQuestion() -- Otherwise change question
end
end
-- Connect all buttons to answer function
local UI_objs=script.Parent.Frame:GetChildren()
for i,obj in pairs(UI_objs) do
if (obj:IsA('TextButton')) then
obj.MouseButton1Click:Connect(function () answer(obj.Name) end)
end
end
changeQuestion()
not sure what you mean by underlined in blue, but at the top heres whats happening.
When your roblox player gets a new character, so your character spawns, it will start listening to every time the player touches an object called ‘CBBrick’. When that happens it tries to call the function startTest(), which should be a function that starts the test. But you don’t seem to have that function. Also put all of this at the end, since code only knows things named above it.
Wouldn’t start test be the same as changeQuestion()? Correct me if I’m wrong.
local function changeQuestion() -- load question local TempQuestionNumber = math.random(1,#questions) local QuestionData = questions[TempQuestionNumber] Question, Answers, CorrectAnswer = unpack(QuestionData) -- Set all frames visibility to false local UI_objs=script.Parent.Frame:GetChildren() for i,obj in pairs(UI_objs) do if (obj:IsA('Frame')) then obj.Visible=false end end
Actually its very close, before using the part to start the test, changeQuestion() was used to change the test to the first question. But its also used to change the question; so if this was connected directly to the part a character touches then the player might change the question without answering. You also want a function to reset the data table every time the test starts, since the data is removed when you change questions.
After an hours worth of researching this is what I’ve got (6 lines of code ). Not sure how many mistakes I’ve made probably a lot, please tell me what I’ve done wrong. Thanks.
local questions = {
{"Red",{"Red","Orange","Yellow","Green","Blue","Purple"},1},
{"Orange",{"Red","Orange","Yellow","Green","Blue","Purple"},2},
{"Yellow",{"Red","Orange","Yellow","Green","Blue","Purple"},3},
{"Green",{"Red","Orange","Yellow","Green","Blue","Purple"},4},
{"Blue",{"Red","Orange","Yellow","Green","Blue","Purple"},5},
{"Purple",{"Red","Orange","Yellow","Green","Blue","Purple"},6}
}
--Reset The Data Table
local function resetData()
table.insert(questions,questions)
end
local numberCorrect = 0
local NumberOfQuestions = 6
local function finish()
local percentCorrect = math.floor(numberCorrect/NumberOfQuestions * 100)
--Disables The Gui
game.Players.LocalPlayer.PlayerGui.ColorBlindLockScreenTest.Frame.Visible = false
game.Players.LocalPlayer.PlayerGui.ColorBlindLockScreenTest.TestEnd.Visible = true
game.Players.LocalPlayer.PlayerGui.ColorBlindLockScreenTest.TestEnd.Number.Text = percentCorrect
end
local Question, Answers, CorrectAnswer
local function changeQuestion()
-- load question
local TempQuestionNumber = math.random(1,#questions)
local QuestionData = questions[TempQuestionNumber]
Question, Answers, CorrectAnswer = unpack(QuestionData)
-- Set all frames visibility to false
local UI_objs=script.Parent.Frame:GetChildren()
for i,obj in pairs(UI_objs) do
if (obj:IsA('Frame')) then
obj.Visible=false
end
end
-- Set the corresponding frame's visibility to true if it exists
if script.Parent.Frame:FindFirstChild(Answers[CorrectAnswer]) then
script.Parent.Frame:FindFirstChild(Answers[CorrectAnswer]).Visible=true
end
table.remove(questions,TempQuestionNumber)
end
local function answer(name)
if string.match(name, Answers[CorrectAnswer]) then -- If button name contains answer than mark correct; works within this specific context, may not work for another
numberCorrect = numberCorrect+1
end
if #questions<=0 then -- If all questions are taken then finish test
finish()
else
changeQuestion() -- Otherwise change question
end
end
-- Connect all buttons to answer function
local UI_objs=script.Parent.Frame:GetChildren()
for i,obj in pairs(UI_objs) do
if (obj:IsA('TextButton')) then
obj.MouseButton1Click:Connect(function () answer(obj.Name) end)
end
end
changeQuestion()
--Starts the test
local function startTest()
resetData()
end
local players=game:GetService('Players')
local player=players.LocalPlayer
player.CharacterAdded:Connect(function (character)
local humanoid=character:WaitForChild('Humanoid')
humanoid.Touched:Connect(function (part)
if part.Name=='CBBrick' then
startTest()
end
end)
end)
lol sorry, thats not gonna work out. You can’t really restore data into a table from the same table.
You want to have a table that doesn’t change and one that does.
Here’s the code put altogether I think your looking for, check out the comments to try and understand how the code works.
local players=game:GetService('Players')
local player=players.LocalPlayer
local questions = { -- Table used to verify which questions are correct
{"Red",{"Red","Orange","Yellow","Green","Blue","Purple"},1},
{"Orange",{"Red","Orange","Yellow","Green","Blue","Purple"},2},
{"Yellow",{"Red","Orange","Yellow","Green","Blue","Purple"},3},
{"Green",{"Red","Orange","Yellow","Green","Blue","Purple"},4},
{"Blue",{"Red","Orange","Yellow","Green","Blue","Purple"},5},
{"Purple",{"Red","Orange","Yellow","Green","Blue","Purple"},6}
}
local current_questions={}
local numberCorrect = 0
local NumberOfQuestions = 6
local testInSession=false
local function finishTest()
-- Calculate number correct
local percentCorrect = math.floor(numberCorrect/NumberOfQuestions * 100)
--Disables The Gui
script.Parent.Questions.Visible = false
script.Parent.Questions.Answers = false
script.Parent.TestEnd.Visible = true
game.Players.LocalPlayer.PlayerGui.ColorBlindLockScreenTest.TestEnd.Number.Text = percentCorrect
wait(5) -- Time to allow player to step off part, before allowing the test to begin again
testInSession=false
end
local Question, Answers, CorrectAnswer
local function changeQuestion()
-- load question
local TempQuestionNumber = math.random(1,#current_questions)
local QuestionData = questions[TempQuestionNumber]
Question, Answers, CorrectAnswer = unpack(QuestionData) -- update these variables with the newest question
-- Set all frames visibility to false
local UI_objs=script.Parent.Questions:GetChildren()
for i,obj in pairs(UI_objs) do
obj.Visible=false
end
-- Set the corresponding question ui obj visibility to true if it exists
if script.Parent.Questions:FindFirstChild(Answers[CorrectAnswer]) then
script.Parent.Questions:FindFirstChild(Answers[CorrectAnswer]).Visible=true
end
table.remove(current_questions,TempQuestionNumber)
end
local function answer(name) -- The answer function has the name of the button pressed
if name==Answers[CorrectAnswer] then -- if that name is equal to the correct answer then add 1 to the number correct
numberCorrect = numberCorrect+1
end
if #current_questions<=0 then -- If all questions are taken then finish test
finishTest()
else
changeQuestion() -- Otherwise change question
end
end
--Starts the test
local function startTest()
-- Enable the UI
script.Parent.Questions.Visible=true
script.Parent.Answers.Visible=true
script.Parent.TestEnd.Visible=false
current_questions={} -- Ensure current questions is empty
for index,value in pairs(questions) do -- For every question
current_questions[index]=value -- Insert it into the current_questions table
end
-- This clones the question table into a new table
numberCorrect=0 -- Reset number correct to 0
testInSession=true -- Set a debounce variable so that the test will not be restarted unless it was finished
changeQuestion() -- Start test by popping a question off the table
end
player.CharacterAdded:Connect(function (character) -- When our character spawns
local humanoid=character:WaitForChild('Humanoid') -- Get the humanoid when its ready
humanoid.Touched:Connect(function (part) -- If the character touches something then pass 'part' into this function
if part.Name=='CBBrick' and testInSession==false then -- Check part name and if player is currently not taking this test
startTest()
end
end)
end)
-- Connect all buttons to answer function
local UI_objs=script.Parent.Answers:GetChildren() -- Get all the answers
for i,obj in pairs(UI_objs) do -- Go through them
if (obj:IsA('TextButton')) then -- Check if there a button
obj.MouseButton1Click:Connect(function () answer(obj.Name) end) -- Connect the button to the answer function; so when the buttons are clicked they call the answer function
end
end
You also want to change the UI structure, because the buttons shouldn’t have the same name as the answers in the same frame. This will confuse the script looking for objects of a certain name, because two exist.
What does upvalue answer mean? I keep getting it as an error. Also it’s saying that there’s something wrong with line 115, but it doesn’t say what’s wrong. I went to look, but I couldn’t find anything wrong with it.
11:08:28.139 - Script 'Players.Play_MazeOfHeck.PlayerGui.ColorBlindLockScreenTest.TestScript', Line 64 - upvalue answer 11:08:28.140 - Script 'Players.Play_MazeOfHeck.PlayerGui.ColorBlindLockScreenTest.TestScript', Line 115
Picture Of My Setup
Thanks for taking your own time out of your day to help me, I hope you have a good rest of your day.
Sir.Play_MazeOfHeck