Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

For loop only goes through once so output array only has one item?

Asked by 1 year ago

My code below is a snippet that is supposed to create a random chance from a list as I'm too lazy to use math.Random but for some reason, in the final for loop, it only loops through it once, and then it outputs a single item in an array. The whole point of this function is to save time and create random chances and I have no way on continuing my project without this function.

Code:

local function chance(list) --list should go from highest likeness to lowest likeness for example: {High, Medium, Low}
    if type(list) == "table" then
        print(list)
        local inputtable = {}
        for i, v in pairs(list) do
            table.insert(inputtable, list[i])
            print(inputtable)
        end
        local outputlength = #list * 3
        print(outputlength)
        local outputtable = {}
        for i, v in pairs(inputtable) do
            print(i)
            print(v)
            for i = 1, outputlength do
                print(i)
                print(inputtable[i])
                table.insert(outputtable, inputtable[i])
        return outputtable
            end
        end 
    else
        print("Input is not a table.")
    end
end

Output:

02:14:39.498   ?  {
[1] = PointOrb,
[2] = RedPointOrb,
[3] = BluePointOrb
}  -  Server - SpawningScript:48
02:14:39.499   ?  {
[1] = PointOrb
}  -  Server - SpawningScript:52
02:14:39.501   ?  {
[1] = PointOrb,
[2] = RedPointOrb
}  -  Server - SpawningScript:52
02:14:39.502   ?  {
[1] = PointOrb,
[2] = RedPointOrb,
[3] = BluePointOrb
}  -  Server - SpawningScript:52
02:14:39.504  9  -  Server - SpawningScript:55
02:14:39.504  1  -  Server - SpawningScript:58
02:14:39.504  PointOrb  -  Server - SpawningScript:59
02:14:39.505  1  -  Server - SpawningScript:61
02:14:39.505  PointOrb  -  Server - SpawningScript:62
02:14:39.506   ?  {
[1] = PointOrb
}  -  Server - SpawningScript:84

1 answer

Log in to vote
0
Answered by 1 year ago

It's because you are returning the output table as soon as you insert an element to it.

Move the return above your else statement

Ad

Answer this question