As the title suggests, how would I achieve this action? I cannot have the same number be used twice, or even more at the same time. Here’s the script:
for i,v in pairs(script.Parent:GetChildren()) do
if v.Name == "t1" or v.Name == "t2" or v.Name == "t3" or v.Name == "t4" then
if v:WaitForChild("IsCorrect").Value ~= true then
v:WaitForChild("Number").Text = numbers[math.random(1,#numbers)]
-- Add something to prevent math.random() from creating the same number in one function call.
end
end
end
local generatedNumbers = {}
local function uniquePseudoRandomGenerator(lower : number, upper : number) : number
if upper - lower + 1 == #generatedNumbers then return end
local randomNumber
repeat
randomNumber = math.random(lower, upper)
until not table.find(generatedNumbers, randomNumber)
table.insert(generatedNumbers, randomNumber)
return randomNumber
end
The return end statement is to prevent indefinite yielding.
Hey, this script works, but I can’t figure out how to work with my numberNames table. Heres the full script:
local numbers = {1,2,3,4,5,6,7,8,9,0}
local numberNames = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Zero"}
local RandomButton = {1,2,3,4}
local generatedNumbers = {}
local QuestionLabel = script.Parent.QuestionLabel
local CorrectNum = script.Parent.CorrectNum
local t1 = script.Parent.t1
local t2 = script.Parent.t2
local t3 = script.Parent.t3
local t4 = script.Parent.t4
local QuestionLabelAnswer
local Correct
local function uniquePseudoRandomGenerator(lower : number, upper : number) : number
if upper - lower + 1 == #generatedNumbers then print("err") end
local randomNumber
repeat
randomNumber = math.random(lower, upper)
until not table.find(generatedNumbers, randomNumber)
table.insert(generatedNumbers, randomNumber)
return randomNumber
end
local function RandomVerify()
task.wait(0.42)
CorrectNum.Value = uniquePseudoRandomGenerator(1,#numberNames) -- Need this to be a NumberName, but throws an error.
QuestionLabel.Text = "Choose number: "..CorrectNum.Value
if CorrectNum.Value == "One" then
QuestionLabelAnswer = 1
elseif CorrectNum.Value == "Two" then
QuestionLabelAnswer = 2
elseif CorrectNum.Value == "Three" then
QuestionLabelAnswer = 3
elseif CorrectNum.Value == "Four" then
QuestionLabelAnswer = 4
elseif CorrectNum.Value == "Five" then
QuestionLabelAnswer = 5
elseif CorrectNum.Value == "Six" then
QuestionLabelAnswer = 6
elseif CorrectNum.Value == "Seven" then
QuestionLabelAnswer = 7
elseif CorrectNum.Value == "Eight" then
QuestionLabelAnswer = 8
elseif CorrectNum.Value == "Nine" then
QuestionLabelAnswer = 9
elseif CorrectNum.Value == "Zero" then
QuestionLabelAnswer = 0
end
Correct = RandomButton[math.random(1,#RandomButton)]
if Correct == 1 then
t1.IsCorrect.Value = true
t1.Number.Text = QuestionLabelAnswer
elseif Correct == 2 then
t2.IsCorrect.Value = true
t2.Number.Text = QuestionLabelAnswer
elseif Correct == 3 then
t3.IsCorrect.Value = true
t3.Number.Text = QuestionLabelAnswer
elseif Correct == 4 then
t4.IsCorrect.Value = true
t4.Number.Text = QuestionLabelAnswer
end
for i,v in pairs(script.Parent:GetChildren()) do
if v.Name == "t1" or v.Name == "t2" or v.Name == "t3" or v.Name == "t4" then
if v:WaitForChild("IsCorrect").Value ~= true then
v:WaitForChild("Number").Text = uniquePseudoRandomGenerator(1,#numbers)
end
end
end
end
t1.MouseButton1Up:Connect(function()
if t1.IsCorrect.Value == true then
task.wait(1)
t1.IsCorrect.Value = false
RandomVerify()
end
end)
t2.MouseButton1Up:Connect(function()
if t2.IsCorrect.Value == true then
task.wait(1)
t2.IsCorrect.Value = false
RandomVerify()
end
end)
t3.MouseButton1Up:Connect(function()
if t3.IsCorrect.Value == true then
task.wait(1)
t3.IsCorrect.Value = false
RandomVerify()
end
end)
t4.MouseButton1Up:Connect(function()
if t4.IsCorrect.Value == true then
task.wait(1)
t4.IsCorrect.Value = false
RandomVerify()
end
end)
RandomVerify()
Wait this doesnt make any sense. The first guy proposed an idea and you said you dont like this idea and then the other guy posts a script that does thr same thing as the first user described.