Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I properly check if a DataStore hasn't been used/ is empty?

Asked by 4 years ago

Recently I've made an addition to a game that allows you to edit your character if you haven't already played the game/if your datastore isn't used yet. Based on this, I have set a value that changes depending on whether or not this person plays the game. If they have played, the value is set to 1. If they haven't played, the value is set to 0. But for some reason, what ends up happening, despite the game telling me that the players datastore is NOT equal to nil, the players value is set to 0, causing them to never be able to play.

Heres the code:


local prefix = 'user_'..Player.UserId local plrData = Data:GetAsync(prefix .. tostring(Player.UserId)) if plrData then print 'is' STVal.Value = 1 QVal.Value = Data:GetAsync(prefix .. tostring(Player.UserId))[1] CVal.Value = Data:GetAsync(prefix .. tostring(Player.UserId))[2] TVal.Value = Data:GetAsync(prefix .. tostring(Player.UserId))[3] SVal.Value = Data:GetAsync(prefix .. tostring(Player.UserId))[4] IVal.Value = Data:GetAsync(prefix .. tostring(Player.UserId))[5] TRVal.Value = Data:GetAsync(prefix .. tostring(Player.UserId))[6] AVal.Value = Data:GetAsync(prefix .. tostring(Player.UserId))[7] ATVal.Value = Data:GetAsync(prefix .. tostring(Player.UserId))[8] HVal.Value = Data:GetAsync(prefix .. tostring(Player.UserId))[9] HCVal.Value = Data:GetAsync(prefix .. tostring(Player.UserId))[10] SCVal.Value = Data:GetAsync(prefix .. tostring(Player.UserId))[11] SHVal.Value = Data:GetAsync(prefix .. tostring(Player.UserId))[12] PAVal.Value = Data:GetAsync(prefix .. tostring(Player.UserId))[13] elseif plrData == nil then print'isnt' STVal.Value = 0 QVal.Value = 0 CVal.Value = 1 TVal.Value = 1 SVal.Value = 1 IVal.Value = 1 TRVal.Value = 1 AVal.Value = 6 ATVal.Value = 30 HVal.Value = 1 HCVal.Value = 1 SCVal.Value = 1 SHVal.Value = 1 PAVal.Value = 1 end

Let me make some things clear. STVal is is the value that I am referring to. The other values can be ignored. I have purposely left some of the code out, to shorten this question. The datastores work fine, it's just that despite a players data not being equal to nil, STVal is still 0! How do I fix this?

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I am not sure this will work but try using elseif not plrData then. This will check if plrData is a thing. If not, then the if statement will run the code inside it. If that doesn't work, just use else.

0
Seems to work for now. Thanks. Wutsinhower 23 — 4y
0
No problem. youtubemasterWOW 2741 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

An easy way of checking if a datastore is empty is just to use GetAsync and see if it returns a value for your request.

Example:

local dataStoreService = game:GetService("DataStoreService")
local coinsData = dataStoreService:GetDataStore("Coins")

game.Players.PlayerAdded:Connect(function(player)
    local playersCoins = coinsData:GetAsync("Coins~"..player.UserId) or nil

    if playersCoins == nil then
        print("Player has no coin data!")
    else
        print(playersCoins)
    end
end)

The reason you don't want to do or 0 is because the player may really have 0 coins in their account because they spent them or something.

But yeah. An easy way to check if a datastore is empty or has a value.

Answer this question