Or operator not working (DataStore)

Hello,
I recently was making a datastore and it was using string values to overcome the inter limit for IntValues, and formatting it for the leaderstats.

However, when a new player joins instead of taking the Default values from the table, it just returns an Index nil error ignoring the “or” statement to take from the Default value from the table

Screenshot of example error

Script
--// Services //--
local DataStore = game:GetService("DataStoreService")

--// Variables //--
local Store = DataStore:GetDataStore("Merge_Test_1")

--// Data Store //--
local function saveData(plr, plrstats)
    local dataToSave = {}

    warn("Saving Data....")

    for _, data in pairs(plrstats:GetChildren()) do
        dataToSave[data.Name] = data.Value
    end

    Store:SetAsync(plr.UserId, dataToSave)
    warn("Data Saved!")

end

local suffixes = {'','K','M','B','T','Qd','Qn','SX','Sp','O','N','de','Ud','DD','tdD','qdD','QnD','sxD','SpD','OcD','NvD','Vgn','UVg','DVg','TVg','qtV','QnV','SeV','SPG','OVG','NVG','TGN','UTG','DTG','tsTG','qtTG','QnTG','ssTG','SpTG','OcTG','NoAG','UnAG','DuAG','TeAG','QdAG','QnAG','SxAG','SpAG','OcAG','NvAG','CT'}

local function format(val)
    local numberedval = tonumber(val)
    for i=1, #suffixes do
        if numberedval < 10^(i*3) then
            return tostring(math.floor(numberedval/((10^((i-1)*3))/100))/(100)..suffixes[i])
        end
    end
end

--// Function //--
game.Players.PlayerAdded:Connect(function(plr)
    local success, plrSaves = pcall(function()
        return Store:GetAsync(plr.UserId)
    end)

    if not success then plr:Kick("Data didn't load") warn("Data failed to load for "..plr.Name) end

    local plrSaves = plrSaves 
    
    local deafualt = {
        ["Money"] = 0,
        ["Gems"] = 0,
        ["Sword"] = 0,
        ["PlayerMulti"] = 1,
    } 
    
    local Leader = Instance.new("Folder")
    Leader.Name = "leaderstatsInt"
    Leader.Parent = plr

    local Money = Instance.new("StringValue")
    Money.Parent = Leader
    Money.Name = "Money"
    Money.Value = plrSaves["Money"] or deafualt

    local Gems = Instance.new("StringValue")
    Gems.Parent = Leader
    Gems.Name = "Gems"
    Gems.Value = plrSaves["Gems"] or deafualt

    local Sword = Instance.new("StringValue")
    Sword.Parent = Leader
    Sword.Name = "Sword"
    Sword.Value = plrSaves["Sword"] or deafualt

    local PlayerMulti = Instance.new("StringValue")
    PlayerMulti.Parent = Leader
    PlayerMulti.Name = "PlayerMulti"
    PlayerMulti.Value = plrSaves["PlayerMulti"] or deafualt

    local LeaderDisplay = Instance.new("Folder")
    LeaderDisplay.Name = "leaderstats"
    LeaderDisplay.Parent = plr

    local MoneyDisplay = Instance.new("StringValue")
    MoneyDisplay.Parent = LeaderDisplay
    MoneyDisplay.Name = "Money"
    MoneyDisplay.Value = plrSaves["Money"] or deafualt

    local GemsDisplay = Instance.new("StringValue")
    GemsDisplay.Parent = LeaderDisplay
    GemsDisplay.Name = "Gems"
    GemsDisplay.Value = plrSaves["Gems"] or deafualt

    local SwordDisplay = Instance.new("StringValue")
    SwordDisplay.Parent = LeaderDisplay
    SwordDisplay.Name = "Sword"
    SwordDisplay.Value = plrSaves["Sword"] or deafualt

    local PlayerMultiDisplay = Instance.new("StringValue")
    PlayerMultiDisplay.Parent = LeaderDisplay
    PlayerMultiDisplay.Name = "PlayerMulti"
    PlayerMultiDisplay.Value = plrSaves["PlayerMulti"] or deafualt


    while wait() do
        MoneyDisplay.Value = Money.Value
        GemsDisplay.Value = Gems.Value
        SwordDisplay.Value = Sword.Value
        PlayerMultiDisplay.Value = PlayerMulti.Value
        MoneyDisplay.Value = format(Money.Value)
        GemsDisplay.Value = format(Gems.Value)
        SwordDisplay.Value = format(Sword.Value)
        PlayerMultiDisplay.Value = format(PlayerMulti.Value)
    end
end)



game.Players.PlayerRemoving:Connect(function(plr)
    local plrstats = plr:WaitForChild("leaderstatsInt")
    saveData(plr, plrstats)
end)

Use deafualt[1] or deafualt[2] when selecting variables from a table, i may be interpreting your issue wrong though.

1 Like

I just had fixed the issue as soon as you replied, I basically changed the code slightly

I added a line

local plrSaves = plrSaves or deafualt

and changed the Values to be called as

local Money = Instance.new("StringValue")
	Money.Parent = Leader
	Money.Name = "Money"
	Money.Value = plrSaves["Money"]

and just reset the datastore because the error saved a nil value for it

1 Like
Money.Value = plrSaves["Money"] or deafualt["Money"]

You’ll need to do this.

Never mind, you did this instead.

local plrSaves = plrSaves or deafualt
1 Like