Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.