DataStore2 - IntValue will not update when DS2 gets data?

Hello, I’m experimenting with DataStore2 by using a dictionary to store all of a player’s data; money, level, etc…

The script below doesn’t update the IntValues placed inside the player. I’ve also tried incrementing the values to see if it was a problem with my GUI’s localscript, but it’s not doing anything.

How do you troubleshoot this?

local DataStore2 = require(1936396537)
DataStore2.Combine("PlayerData", "userData")

local defaultData = {
	Core = {
		["Money"] = 1000;
		["Level"] = 1;
		["Perks"] = {nil};
	};

	Clothing = {
		["Pants"] = {nil};
		["Shirts"] = {nil};
		["Hairs"] = {nil};
		["Hats"] = {nil};
		["Masks"] = {nil};
		["Backs"] = {nil};
		["Necks"] = {nil};
	};

	Items = {
		["Tools"] = {nil};
		["Consumables"] = {nil};
	};

	Equipped = {
		["Face"] = {nil};
		["Pants"] = {nil};
		["Shirt"] = {nil};
		["Hair"] = {nil};
		["Hat"] = {nil};
		["Mask"] = {nil};
		["Back"] = {nil};
		["Neck"] = {nil};
	};
}

game.Players.PlayerAdded:Connect(function(player)
	local userDataStore = DataStore2("userData", player)
	local Money = Instance.new("IntValue")
	local Level = Instance.new("IntValue")
	Money.Name = "Money"
	Level.Name = "Level"
	Money.Parent = player
	Level.Parent = player
	
	local userData
	
	local function updateUserData(newValue)
		userData = userDataStore:Get(newValue)
	end
	
	local success, msg = pcall(function()
		updateUserData(defaultData)
	end)
	
	if success then
		userDataStore:OnUpdate(updateUserData)
		Money.Value = userData["Money"]
		Level.Value = userData["Level"]
		player:LoadCharacter()
	else
		print(msg)
	end
end)
local function updateUserData(newValue)
	userData = newValue 
end

try this

I think you need some things moved to the “updateUserData” function to make it work how you want it to. This might fix it:

local function updateUserData(newValue)
    userData = userDataStore:Get(newValue)
    Money.Value = userData["Money"]
    Level.Value = userData["Level"]
end

Okay so I found out that userData[“Money”] and userData[“Level”] equal to nil.
I’m not sure how dictionaries work, but I’m pretty sure:

Money.Value = userData["Money"]

That this is the way to call a value inside a dictionary. Otherwise, what other way can I call a value from a dictionary?

I’m also wondering, is there something wrong with my method of setting the user’s data to the default data if userData is nil?

Here’s the updated code…

local DataStore2 = require(1936396537)
DataStore2.Combine("PlayerData", "userData")

local defaultData = {
	Core = {
		["Money"] = 1000;
		["Level"] = 1;
		["Perks"] = {nil};
	};

	Clothing = {
		["Pants"] = {nil};
		["Shirts"] = {nil};
		["Hairs"] = {nil};
		["Hats"] = {nil};
		["Masks"] = {nil};
		["Backs"] = {nil};
		["Necks"] = {nil};
	};

	Items = {
		["Tools"] = {nil};
		["Consumables"] = {nil};
	};

	Equipped = {
		["Face"] = {nil};
		["Pants"] = {nil};
		["Shirt"] = {nil};
		["Hair"] = {nil};
		["Hat"] = {nil};
		["Mask"] = {nil};
		["Back"] = {nil};
		["Neck"] = {nil};
	};
}

game.Players.PlayerAdded:Connect(function(player)
	local userDataStore = DataStore2("userData", player)
	local Money = Instance.new("IntValue")
	local Level = Instance.new("IntValue")
	Money.Name = "Money"
	Level.Name = "Level"
	Money.Parent = player
	Level.Parent = player
	
	local userData
	
	local function updateUserData(newValue)
		userData = userDataStore:Get(newValue)
		Money.Value = userData["Money"]
		Money.Value = userData["Level"]
	end
	
	local success, msg = pcall(function()
		updateUserData(defaultData)
	end)
	
	if success then
		player:LoadCharacter()
	else
		error(msg)
	end
	
	userDataStore:OnUpdate(updateUserData)
	
end)

I’ve gone ahead and removed the tables out of the dictionary. It’s working fine without the tables inside, and I can see the money and level change as soon as I start the test.

Thanks for your support, I’ll give any future viewer the code, just to help out future members.
Here’s the code:

local DataStore2 = require(1936396537)
DataStore2.Combine("PlayerDataMasterKey", "Core", "Clothes", "Items", "Equipped")

local defaultCore = {
	["Money"] = 1000;
	["Level"] = 1;
	["Perks"] = {};
}

local defaultClothes = {
	["Pants"] = {nil};
	["Shirts"] = {nil};
	["Hairs"] = {nil};
	["Hats"] = {nil};
	["Masks"] = {nil};
	["Backs"] = {nil};
	["Necks"] = {nil};
}

local defaultItems = {
	["Tools"] = {nil};
	["Consumables"] = {nil};
}

local defaultEquipped = {
	["Face"] = {nil};
	["Pants"] = {nil};
	["Shirt"] = {nil};
	["Hair"] = {nil};
	["Hat"] = {nil};
	["Mask"] = {nil};
	["Back"] = {nil};
	["Neck"] = {nil};
}

game.Players.PlayerAdded:Connect(function(player)
	local coreDataStore = DataStore2("Core", player)
	local clothesDataStore = DataStore2("Clothes", player)
	local itemsDataStore = DataStore2("Items", player)
	local equippedDataStore = DataStore2("Equipped", player)
	local Money = Instance.new("IntValue")
	local Level = Instance.new("IntValue")
	Money.Name = "Money"
	Level.Name = "Level"
	Money.Parent = player
	Level.Parent = player
	
	local coreData
	local clothesData
	local itemsData
	local equippedData
	
	local function updateCoreData(newValue)
		coreData = coreDataStore:Get(newValue)
		Money.Value = coreData["Money"]
		Level.Value = coreData["Level"]
	end
	
	local function updateClothesData(newValue)
		clothesData = clothesDataStore:Get(newValue)
	end
	
	local function updateItemsData(newValue)
		itemsData = itemsDataStore:Get(newValue)
	end
	
	local function updateEquippedData(newValue)
		equippedData = equippedDataStore:Get(newValue)
	end
	
	local success, msg = pcall(function()
		updateCoreData(defaultCore)
		updateClothesData(defaultClothes)
		updateItemsData(defaultItems)
		updateEquippedData(defaultEquipped)
	end)
	
	if success then
		player:LoadCharacter()
	else
		error(msg)
	end
	
	coreDataStore:OnUpdate(updateCoreData)
	clothesDataStore:OnUpdate(updateClothesData)
	itemsDataStore:OnUpdate(updateItemsData)
	equippedDataStore:OnUpdate(updateEquippedData)
	
end)