Stat value starting at 0, despite being set to 1?

So ive added 3 stats to my game, and when adding them in it sets their value to 1, and when loading data if they have no data for those stats then its value is also set 1, yet when loading into the game the stat starts at 0
Adding the stats:
image
Loading data:
image

So if the stat is set to default at 1 when its created, and if theirs no data loaded for the player then how does it end up being 0?


(Ive also tested on a local server with multiple players to confirm its an issue, rather than having previously saved data on my account before setting the default value to 1)

Use Control+Shift+F to find all statements that modifies the value, maybe there’s some hidden script that’s intervening

I only added it the other day, so I know theirs no other code which messes or even mentions this value

Does data return nil or an empty table?

The data works for every other stat and value in the game and even this one (changing it on the server to 2 and reconnecting will load 2) so I dont think the data is part of the problem

It’s a simple question. If the data doesn’t load, does it become an empty table or a nil value? If it’s an empty table then that if statement won’t run

This is how its set out
image

The problem seems to be how your if statement is structured:

The first check is when the data request is successful. Therefore, any elseif conditions following that assumes that “success” is not true, yet in that elseif condition you are checking for “success” being true

You should instead immediately check for the absence of data right after confirming that the data request was successful, and go on from there

if success then
  if data then

  else

Also, perhaps you were supposed to do this instead:

if success and data then

else
1 Like

what if you try like this

local success, data = pcall(statstore.GetAsync, statstore, playerUserID)

if success == true then
    data = data or {}
    player.extrastats.XP.Value = data.xpData or 0
    player.upgradestats.M4A1Damage.Value = data.M4A1DamageData or 1
    ...
else
    print(data)
end
1 Like

This looks useful to improving how its set out, but I still fail to see how the value would be set to 0? If you dont have data it should be 1, and if you do or if its not set out in a way to set it to 1 (in the elseif section), it should still be 1 as thats the value its given when its created

This also looks like a cleaner way to sort it, thanks

if the value is already set to 0 in the datastore then it will be stuck at 0 until the datastore has been fixed

Even when I load in with a player who has never played before?
I thought if it was set to 0 then changed to 1 in the datastore (as it is now) then any new player would be given the new value of 1

in your code your doing

if success then
    -- code 1
elseif success and not data then
    -- code 2
end

but it will always go into code 1 even for new players because data = nil new players will error and there value will stay at 0

its impossible to go into code 2

1 Like

I see thanks, I just find it weird that other stats which are set out in the exact same way (set as 1 when created, set as 1 if no data) work while this one doesnt

-- lua checks to see if success is = to true if it is then it will run code 1 but if success is not true then it will skip
if success then
    -- code 1
elseif success and not data then -- if success was false and we skipped the (if success then) now lua will check this and this is saying if success is true but success will never be true becuase if it was true it would of went into code 1
    -- code 2
end

its a bit hard to explain but if success is true then it will not go into else

local hairColor = "Red"
if hairColor == "Red" then
    print("Its RED!!!")
else
    print("Its Not RED!!!")
end

if hairColor == "Red" then
    print("Its RED!!!")
elseif hairColor == "Red" then
    print("???") -- this can never happen
end
1 Like

Ive just added a new player and their value is still set to 0


Though ive changed the datastore to this:
image

one problem i see is while data might exist

data.xpData might be nil

then it will error

I see how that could cause an issue with it being loaded 1 line at a time, so if I implement your style like this:


Would it be safer?

yes because in the future if you add more values then old players who don’t have the value in there datastore yet will get a error

1 Like