#if statements not working?? (fixed)

1 messages · Page 1 of 1 (latest)

stoic stag
#

hello so idk what and why but it doesnt work with this code?

local function FindTotalPetsNumber(Table)
    print(Table)
    local TempCounter = 0
    
    for i, v in pairs(Table) do
        print(player.UserId, i)
        
        if player.UserId == i then
            print("found")
            
            for y, x in pairs(v) do
                TempCounter += x
            end
        end
    end
    print(TempCounter) -- it prints 0
    return TempCounter
end

it prints until the print(player.UserId, i) even though player.UserId is the same as i.

am i dumb and not understanding something?

stoic stag
#

if statements not working??

native copper
#

In the screenshot “i” is a string not a number

stoic stag
#

oh right

native copper
#

so you could do tostring(player.UserId) or tonumber(i)

stoic stag
#

yeah thanks

#

if statements not working?? (fixed)

native copper
#

Also can’t you do

local sum = 0
local playerdataOrWhateverThisIs = Table[player.UserId] or false
if playerdataOrWhateverThisIs == false then return end
for _, dataCount: number in pairs(playerdataOrWhateverThisIs) do
sum += dataCount
end

#

sorry I’m on mobile right now so I couldn’t format the code

#

To avoid the nesting or whatever

#

Doesn’t change much honestly it’s whatever you prefer

stoic stag
#

are for loops slowing the code?

native copper
#

No in what I sent there is also a for loop

native copper
native copper
stoic stag
#

so if local playerthing = table[player.UserId] is nil then its gonna be false?

native copper
#

yeah

stoic stag
#

and wait what is for _, "dataCount:number" ?

native copper
#

you’d still have to do tostring(player.UserId)

native copper
stoic stag
#

even with the column?

native copper
#

The i and v are variable names though so you can change it to whatever you want

stoic stag
#

i meant the value not the index of that for loop

native copper
#

the semicolon basically says that Datacount is a number data type

stoic stag
#

oh

native copper
native copper
stoic stag
#

oh alr thx

native copper
#

It makes no difference when the script is ran at all

#

Yeah no problem

#

Don’t change your code if you feel it’s easier to understand the way you do it already

#

It’s not worth the worry since you get the same output

stoic stag
#

yeah but its good to not have that many for loops

#

or even nested for loops