#Returned Table being different than the assigned Table for some reason.

1 messages · Page 1 of 1 (latest)

verbal nacelle
#

Code and Video Footage:

#

Code:

local PossibleQuestNames = {"Hits","Kills","Blocks","PerfectBlocks","PerfectBlockStreak","BlockBreaks"}

local function ChooseRandomNumbers(NumA, NumB) --Num stands for Number
    task.wait(0.01)
    local Numbers = {math.random(NumA, NumB), math.random(NumA, NumB), math.random(NumA, NumB)}
    for Index, Number in pairs(Numbers) do
        print(Number)
    end
    if Numbers[1] == Numbers[2] or Numbers[1] == Numbers[3] or Numbers[2] == Numbers[3] or Numbers[2] == Numbers[1] then
        print("Same Number detected")
        ChooseRandomNumbers(NumA, NumB)
    end
    return Numbers
end

local function ChooseQuests()
    local QuestNames = {}
    task.wait(0.01)
    local Numbers = ChooseRandomNumbers(1,3)
    print("| Number Choosing done |")
    
    for Index, Number in pairs(Numbers) do
        print(Number)
        table.insert(QuestNames, PossibleQuestNames[Number])
    end
    print("| Quest Choosing done |")
    
    for Index, QuestName in pairs(QuestNames) do
        print(QuestName)
    end
end
#

Extra screenshot of the code

desert obsidian
verbal nacelle
#

so wait. Does that mean that the Table i assign in the ChooseQuest function has always the values of the first ChooseRandomNumbers function Table?

desert obsidian
#

yes

#

because that call just calls the function itself and creates a new one completely different but that table is not used

#

also the check can be really easy to do like this:

#
local function isValueRepeatedInTable(table: {[any]: any}): boolean
    local seen = {}

    for _, v in table do
        if seen[v] then
            return true
        end
        seen[v] = true
    end

    return false 
end

#

and just do if isValueRepeatedInTable(t) do...

verbal nacelle
#

wait but if the function i call will always be a new one how can i repeat the function until isValueRepeatedInTable() == false?

desert obsidian
#

just do return funcCall() in the if

#

here

#

return ChooseRandomNumbers(NumA, NumB)

verbal nacelle
#

aaa i see

#

thank you for helping. Btw what does this syntax mean? I have never seen the "(table: {[any]: any}): boolean"

desert obsidian
#

that's type annotation, basically it's like a guide with what types are you working and what types are you returning, for more info: https://luau.org/typecheck