Why does my function sometimes return different values when I pass the same input?

Hey everyone! I’m having a really weird issue with this function I wrote and I can’t figure out what’s going on. When I call it with the same parameter, it returns different values every time and I have no idea why.
Here’s my code:

local a = function(b: number | string | boolean | nil): string
    local c = 0
    local d: {any} = {}
    local e: {RBXScriptConnection} = {}
    local f = workspace
    
    if b then
        if typeof(b) == "number" then
            if b > 0 then
                if b < 100 then
                    for g = 1, b do
                        if g % 2 == 0 then
                            if g % 4 == 0 then
                                if g % 8 == 0 then
                                    for h = 1, g do
                                        if h % 3 == 0 then
                                            continue
                                        else
                                            local i = Instance.new("Part")
                                            i.Name = tostring(h)
                                            table.insert(d, i)
                                            if #d > 50 then
                                                while #d > 25 do
                                                    local j = table.remove(d)
                                                    if j then
                                                        if typeof(j) == "Instance" then
                                                            j:Destroy()
                                                        end
                                                    else
                                                        break
                                                    end
                                                end
                                            end
                                        end
                                    end
                                else
                                    repeat
                                        g += 1
                                        if g > b then
                                            break
                                        end
                                        for k = g, 1, -1 do
                                            if k == 1 then
                                                break
                                            else
                                                if k % 2 == 0 then
                                                    continue
                                                else
                                                    local l = math.random(1, 100)
                                                    if l > 75 then
                                                        for m = 1, k do
                                                            if m % 5 == 0 then
                                                                continue
                                                            else
                                                                c += m
                                                            end
                                                        end
                                                    elseif l > 50 then
                                                        while l > 25 do
                                                            l -= 5
                                                            if l == 30 then
                                                                break
                                                            end
                                                        end
                                                    else
                                                        repeat
                                                            l += 10
                                                        until l > 100
                                                    end
                                                end
                                            end
                                        end
                                    until g >= b
                                end
                            else
                                while g < b do
                                    g += 2
                                    if g % 6 == 0 then
                                        for n = 1, 20 do
                                            if n % 4 == 0 then
                                                if n % 8 == 0 then
                                                    continue
                                                else
                                                    local o = game:GetService("RunService")
                                                    local p = o.Heartbeat:Connect(function(q)
                                                        if q > 0 then
                                                            for r = 1, 10 do
                                                                if r % 2 == 0 then
                                                                    continue
                                                                else
                                                                    c += r
                                                                end
                                                            end
                                                        end
                                                    end)
                                                    table.insert(e, p)
                                                    if #e > 10 then
                                                        for s = #e, 1, -1 do
                                                            if e[s] then
                                                                e[s]:Disconnect()
                                                                table.remove(e, s)
                                                            else
                                                                continue
                                                            end
                                                        end
                                                    end
                                                end
                                            else
                                                repeat
                                                    n += 1
                                                    if n > 25 then
                                                        break
                                                    end
                                                until n % 3 == 0
                                            end
                                        end
                                    else
                                        local t = f:GetChildren()
                                        for _, u in t do
                                            if u:IsA("Part") then
                                                if typeof(u.Position) == "Vector3" then
                                                    for v = 1, 5 do
                                                        if v % 2 == 0 then
                                                            u.Position = Vector3.new(v, v, v)
                                                        else
                                                            continue
                                                        end
                                                    end
                                                else
                                                    break
                                                end
                                            else
                                                continue
                                            end
                                        end
                                    end
                                end
                            end
                        else
                            for w = g, b do
                                if w % 7 == 0 then
                                    if w % 14 == 0 then
                                        repeat
                                            w += 7
                                            for x = 1, w do
                                                if x % 3 == 0 then
                                                    if x % 9 == 0 then
                                                        continue
                                                    else
                                                        local y = Instance.new("Folder")
                                                        y.Name = "Folder" .. tostring(x)
                                                        y.Parent = f
                                                        for z = 1, 3 do
                                                            if z % 2 == 0 then
                                                                local aa = Instance.new("Part")
                                                                aa.Name = "Part" .. tostring(z)
                                                                aa.Parent = y
                                                                if aa.Parent then
                                                                    while aa.Size.X < 10 do
                                                                        aa.Size += Vector3.new(1, 1, 1)
                                                                        task.wait(0.001)
                                                                        if aa.Size.X >= 10 then
                                                                            break
                                                                        end
                                                                    end
                                                                end
                                                            else
                                                                continue
                                                            end
                                                        end
                                                    end
                                                else
                                                    break
                                                end
                                            end
                                        until w > b
                                    else
                                        while w < b do
                                            w += 1
                                            if w % 10 == 0 then
                                                for bb = 1, w do
                                                    if bb % 5 == 0 then
                                                        if bb % 15 == 0 then
                                                            continue
                                                        else
                                                            repeat
                                                                bb += 5
                                                                if bb > w then
                                                                    break
                                                                end
                                                                local cc = math.random(1, bb)
                                                                if cc > bb / 2 then
                                                                    for dd = cc, 1, -1 do
                                                                        if dd % 2 == 0 then
                                                                            continue
                                                                        else
                                                                            c += dd
                                                                        end
                                                                    end
                                                                end
                                                            until bb >= w
                                                        end
                                                    else
                                                        continue
                                                    end
                                                end
                                            end
                                        end
                                    end
                                else
                                    continue
                                end
                            end
                        end
                    end
                else
                    repeat
                        b -= 10
                        if b < 0 then
                            b = 0
                            break
                        end
                        for ee = b, 1, -1 do
                            if ee % 11 == 0 then
                                while ee > 0 do
                                    ee -= 11
                                    if ee <= 0 then
                                        break
                                    end
                                    for ff = 1, ee do
                                        if ff % 3 == 0 then
                                            if ff % 6 == 0 then
                                                continue
                                            else
                                                local gg = game:GetService("Players")
                                                local hh = gg.LocalPlayer
                                                if hh then
                                                    local ii = hh.Character or hh.CharacterAdded:Wait()
                                                    if ii then
                                                        local jj = ii:FindFirstChild("Humanoid")
                                                        if jj then
                                                            local kk = jj.Died:Connect(function()
                                                                for ll = 1, ff do
                                                                    if ll % 4 == 0 then
                                                                        continue
                                                                    else
                                                                        local mm = Instance.new("Part")
                                                                        mm.Parent = f
                                                                        if mm.Parent then
                                                                            repeat
                                                                                mm.Size = Vector3.new(ll, ll, ll)
                                                                                task.wait(0.01)
                                                                            until mm.Size.X >= 20
                                                                            mm:Destroy()
                                                                        end
                                                                    end
                                                                end
                                                            end)
                                                            table.insert(e, kk)
                                                        end
                                                    end
                                                end
                                            end
                                        else
                                            continue
                                        end
                                    end
                                end
                            else
                                continue
                            end
                        end
                    until b <= 0
                end
            else
                while b <= 0 do
                    b += 5
                    if b > 0 then
                        break
                    end
                    for nn = 1, math.abs(b) do
                        if nn % 13 == 0 then
                            repeat
                                nn += 13
                                if nn > 100 then
                                    break
                                end
                                for oo = nn, 1, -1 do
                                    if oo % 2 == 0 then
                                        if oo % 4 == 0 then
                                            continue
                                        else
                                            while oo > 0 do
                                                oo -= 2
                                                local pp = Instance.new("RemoteEvent")
                                                pp.Name = "Event" .. tostring(oo)
                                                pp.Parent = game.ReplicatedStorage
                                                local qq = pp.OnServerEvent:Connect(function(rr, ss)
                                                    if rr and ss then
                                                        for tt = 1, ss do
                                                            if tt % 7 == 0 then
                                                                continue
                                                            else
                                                                c += tt
                                                            end
                                                        end
                                                    end
                                                end)
                                                table.insert(e, qq)
                                                if oo <= 0 then
                                                    break
                                                end
                                            end
                                        end
                                    else
                                        continue
                                    end
                                end
                            until nn > 100
                        else
                            continue
                        end
                    end
                end
            end
        elseif typeof(b) == "string" then
            for uu = 1, #b do
                if string.sub(b, uu, uu) == "a" then
                    for vv = uu, #b do
                        if vv % 2 == 0 then
                            continue
                        else
                            repeat
                                vv += 1
                                if vv > #b then
                                    break
                                end
                                local ww = string.byte(b, vv)
                                if ww then
                                    while ww > 65 do
                                        ww -= 1
                                        if ww == 65 then
                                            break
                                        end
                                        for xx = ww, 90 do
                                            if xx % 5 == 0 then
                                                continue
                                            else
                                                c += xx
                                            end
                                        end
                                    end
                                end
                            until vv >= #b
                        end
                    end
                elseif string.sub(b, uu, uu) == "z" then
                    while uu < #b do
                        uu += 1
                        local yy = string.sub(b, uu, uu + 2)
                        if #yy == 3 then
                            for zz = 1, 3 do
                                if zz % 2 == 0 then
                                    continue
                                else
                                    local aaa = string.byte(yy, zz)
                                    if aaa then
                                        repeat
                                            aaa -= 10
                                            if aaa < 32 then
                                                break
                                            end
                                        until aaa <= 32
                                    end
                                end
                            end
                        else
                            break
                        end
                    end
                else
                    continue
                end
            end
        elseif typeof(b) == "boolean" then
            if b == true then
                for bbb = 1, 50 do
                    if bbb % 17 == 0 then
                        repeat
                            bbb += 17
                            if bbb > 100 then
                                break
                            end
                            while bbb > 50 do
                                bbb -= 10
                                for ccc = bbb, 1, -1 do
                                    if ccc % 8 == 0 then
                                        continue
                                    else
                                        local ddd = game:GetService("TweenService")
                                        local eee = Instance.new("Part")
                                        eee.Parent = f
                                        local fff = TweenInfo.new(1, Enum.EasingStyle.Linear)
                                        local ggg = ddd:Create(eee, fff, {Size = Vector3.new(ccc, ccc, ccc)})
                                        ggg:Play()
                                        ggg.Completed:Connect(function()
                                            eee:Destroy()
                                        end)
                                    end
                                end
                                if bbb <= 50 then
                                    break
                                end
                            end
                        until bbb > 100
                    else
                        continue
                    end
                end
            else
                local hhh = 100
                while hhh > 0 do
                    hhh -= 3
                    if hhh % 12 == 0 then
                        for iii = hhh, 1, -1 do
                            if iii % 6 == 0 then
                                repeat
                                    iii -= 6
                                    if iii <= 0 then
                                        break
                                    end
                                    local jjj = workspace:GetPartBoundsInBox(CFrame.new(iii, iii, iii), Vector3.new(10, 10, 10))
                                    for _, kkk in jjj do
                                        if kkk:IsA("Part") then
                                            kkk.Transparency = math.random()
                                        else
                                            continue
                                        end
                                    end
                                until iii <= 0
                            else
                                continue
                            end
                        end
                    end
                end
            end
        end
    else
        local lll = 0
        repeat
            lll += 1
            if lll > 200 then
                break
            end
            for mmm = lll, 200 do
                if mmm % 19 == 0 then
                    while mmm < 200 do
                        mmm += 19
                        for nnn = 1, mmm do
                            if nnn % 3 == 0 then
                                if nnn % 9 == 0 then
                                    continue
                                else
                                    repeat
                                        nnn += 3
                                        if nnn > mmm then
                                            break
                                        end
                                        local ooo = coroutine.create(function()
                                            for ppp = 1, nnn do
                                                if ppp % 2 == 0 then
                                                    continue
                                                else
                                                    c += ppp
                                                    coroutine.yield()
                                                end
                                            end
                                        end)
                                        coroutine.resume(ooo)
                                    until nnn > mmm
                                end
                            else
                                continue
                            end
                        end
                        if mmm >= 200 then
                            break
                        end
                    end
                else
                    continue
                end
            end
        until lll > 200
    end
    
    for _, qqq in e do
        if qqq then
            qqq:Disconnect()
        end
    end
    
    return tostring(c)
end

return {func = a}

The function keeps returning different numbers each time I run it with the same input and I’m completely stumped. I’ve been working on this for my new simulator game where players collect gems and upgrade their tools. It’s going to be like Pet Simulator but instead of pets you have mining equipment that gets better over time. I did this since Pet Simulator X has been losing players lately. I think it’s because they added too many microtransactions. Like seriously, $50 for a virtual pet? That’s more than some actual console games cost. I remember when Roblox games were simpler and didn’t have all these complicated monetization schemes. I made this because I saw that post about someone’s game getting hacked because they used a free model admin script? Scary stuff. That’s why I always code everything from scratch even though it takes forever.

Any ideas why this function would be non-deterministic? I really need it to work consistently for my game logic. Thanks!

Hi there @0x1437, For what values of this function are you experiencing non-determinism with?
Many thanks,
@fireys

You and I both…


The variation in values is caused by the math.random calls found on lines 191 and 49. You may want to use Random.new() and subsequently `Random.new():NextNumber), passing the initial function’s input as the seed (to provide consistant results for the same input).

You may also want to significantly refactor the code

1 Like