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

Why does my datastore value return as nil when i set its value to ""?

Asked by 4 years ago
Edited 4 years ago
local function setupdata(player)
    local playeruserid = player.UserId
    local data = PlayerDataStore:GetAsync(player.UserId)
    if data then
        PlayerData[player.UserId] = data
    else
        PlayerData[player.UserId] = {
            Head = "";
            Neck = "";
            Waist = "";
            Back = "";
            Currency = 0; --default the currency to 0
            Inventory = {}; --default to an empty inventory
        }
    end
    rfunc:FireClient(player, PlayerData[player.UserId]["Inventory"])
end
local function noob(player,humanoid, defaultHumanoidDescription)
    print(PlayerData[player.UserId]["Head"])    
    print(PlayerData[player.UserId]["Back"])    
    defaultHumanoidDescription.HatAccessory = PlayerData[player.UserId]["Head"]
    defaultHumanoidDescription.NeckAccessory = PlayerData[player.UserId]["Neck"]
    defaultHumanoidDescription.WaistAccessory = PlayerData[player.UserId]["Waist"]
    defaultHumanoidDescription.BackAccessory = PlayerData[player.UserId]["Back"]
addhead.OnServerEvent:Connect(function(player, aid)
    print(aid)
    local Inventory = PlayerData[player.UserId]["Inventory"]
    for i, v in pairs (Inventory) do
        if tonumber(aid) == tonumber(v) then
            PlayerData[player.UserId]["Head"] = aid
            local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")
            local defaultHumanoidDescription = game.Players:GetHumanoidDescriptionFromUserId(player.UserId)
            noob(player,humanoid, defaultHumanoidDescription)   
            humanoid:ApplyDescription(defaultHumanoidDescription)
            print(aid)
        end
    end
end)

so since I gave the head datastore a value, the script can read it fine, but the ones I previously defined as "" get read as nil? can I not set my datastore values to ""?

2 answers

Log in to vote
0
Answered by
zomspi 541 Moderation Voter
4 years ago

"" == nil

0
No alexfinger21 341 — 4y
0
Nil is value not set, while "" will be equal to () in the output. Trust me, I tested. alexfinger21 341 — 4y
0
ok zomspi 541 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Hello! That is because in an array, 1 is not the same as "1". So, the script does not understand what you mean. So, the fix to that problem would be just using the index. Somewhat like this:


print(PlayerData[player.UserId][1])

Also, if you don’t know what an index is, the index is the count of the object in an array, so for example, table = {"Hello"}. The onset of the word hello is 1, unless you specified otherwise. Hope it helps!

Answer this question