Int value isnt working

local backpackDict = { -- Makes dictionary of backpacks
    ["Small Backpack"] = 10,
    ["Backpack"] = 20,
    ["Large Backpack"] = 50,
    ["XL Backpack"] = 100,
    ["Duffle Bag"] = 250,
    ["Infinite Backpack"] = 999999999999
}
function leaderstats() -- Declaring function
    game.Players.PlayerAdded:Connect(function(player)
        local leaderstats = Instance.new("Folder",player) -- Creates leaderstats folder inside of player
        leaderstats.Name = "leaderstats"
    
        local coins = Instance.new("IntValue",leaderstats) -- Creates coins value inside of leaderstats
        coins.Name = "Coins"

        local gems = Instance.new("IntValue",leaderstats) -- Creates gems value inside of leaderstats
        gems.Name = "Gems"

        local hatched = Instance.new("IntValue",leaderstats) -- Creates hatched value inside of leaderstats
        hatched.Name = "Hatched"

        local playerStats = Instance.new("Folder",player)
        playerStats.Name = "Stats"
        
        local clicks = Instance.new("IntValue",playerStats)
        clicks.Name = "Clicks"
        
        local backpackSize = Instance.new("IntValue",playerStats)
        backpackSize.Name = "BackpackSize"
        
        local backpack = Instance.new("StringValue",playerStats)
        backpack.Name = "Backpack"
        
        
        local data = playerData:GetAsync(player.UserId) -- Gets player data

        if data ~= nil then -- If there is data then load data or else set everything to 0
            coins.Value = data[1]
            gems.Value = data[2]
            hatched.Value = data[3]
            clicks.Value = data[4]
            backpack.Value = data[5]
        else
            coins.Value = 0
            gems.Value = 0
            hatched.Value = 0
            clicks.Value = 0
            backpack.Value = "Small Backpack"
        end
        
        backpackSize = backpackDict[backpack.Value]
    end)
end

Why is backpack size being set to 0 when backpack is Small Backpack

Add a print before the backpackSize line to see what backpack.Value prints.

The code works for me when I comment out the data line, and I call leaderstats() before the player joins. Printing backpackSize returns 10 for me.

Your code that is not shown might have some issues when making the GetAsync() call because it yields or could error.

Mine prints 0 for some reason, I have no idea why

I said backpacksize not backbacksize.Value

Try this. All I did is comment out the data line, add a print, and call the initialize function at the bottom. And that code works. My guess is that your data isn’t saving or loading right. I’d suggest double checking that the data[5] is in the backpackDict and using a fallback option if it isn’t.

local backpackDict = { -- Makes dictionary of backpacks
    ["Small Backpack"] = 10,
    ["Backpack"] = 20,
    ["Large Backpack"] = 50,
    ["XL Backpack"] = 100,
    ["Duffle Bag"] = 250,
    ["Infinite Backpack"] = 999999999999
}
function leaderstats() -- Declaring function
    game.Players.PlayerAdded:Connect(function(player)
        local leaderstats = Instance.new("Folder",player) -- Creates leaderstats folder inside of player
        leaderstats.Name = "leaderstats"
    
        local coins = Instance.new("IntValue",leaderstats) -- Creates coins value inside of leaderstats
        coins.Name = "Coins"

        local gems = Instance.new("IntValue",leaderstats) -- Creates gems value inside of leaderstats
        gems.Name = "Gems"

        local hatched = Instance.new("IntValue",leaderstats) -- Creates hatched value inside of leaderstats
        hatched.Name = "Hatched"

        local playerStats = Instance.new("Folder",player)
        playerStats.Name = "Stats"
        
        local clicks = Instance.new("IntValue",playerStats)
        clicks.Name = "Clicks"
        
        local backpackSize = Instance.new("IntValue",playerStats)
        backpackSize.Name = "BackpackSize"
        
        local backpack = Instance.new("StringValue",playerStats)
        backpack.Name = "Backpack"
        
        
        local data = nil--playerData:GetAsync(player.UserId) -- Gets player data

        if data ~= nil then -- If there is data then load data or else set everything to 0
            coins.Value = data[1]
            gems.Value = data[2]
            hatched.Value = data[3]
            clicks.Value = data[4]
            backpack.Value = data[5]
        else
            coins.Value = 0
            gems.Value = 0
            hatched.Value = 0
            clicks.Value = 0
            backpack.Value = "Small Backpack"
        end
        
        backpackSize = backpackDict[backpack.Value]
		print('backpackSize:', backpackSize)
    end)
end
leaderstats()

print(backpack.Value)

Try this, the stored string value likely doesn’t correspond with any of the intended backpacks which exist within the “BackpackDict” table.