DataStore2 Table Error

I keep getting an error code saying that the Stat that I’m getting from a table is nil but it shouldn’t be.
image

This is my data Store script

local DataStore2 = require(script.Parent.DataStore2)
local Players = game:GetService(“Players”)
local userDataStoreName = “MasterKey”

local function setupUserData()
local userData = {

	Stat = {
		["Cash"] = 0;
		["Gems"] = 0;
	}
}
print(userData)
return userData

end

DataStore2.Combine(userDataStoreName, “Stat”)

Players.PlayerAdded:Connect(function(player)
local userData = DataStore2(userDataStoreName, player):Get(setupUserData())

local LS = Instance.new("Folder")
LS.Name = "leaderstats"

local Cash = Instance.new("IntValue")
Cash.Name = "Cash"

local Gems = Instance.new("IntValue")
Gems.Name = "Gems"

local StatData = DataStore2("Stat", player)

local function UpdateStats(NewValue)
	Cash.Value = DataStore2(userDataStoreName, player):Get(NewValue)["Stat"]["Cash"]
	Gems.Value = DataStore2(userDataStoreName, player):Get(NewValue)["Stat"]["Gems"]
end

UpdateStats(userData.Stat)

StatData:OnUpdate(UpdateStats)

LS.Parent = player
Cash.Parent = LS
Gems.Parent = LS

wait(5)

userData.Stat.Cash += 3
userData.Stat.Gems += 1
print(userData.Stat)

StatData:Set(userData.Stat)

Cash.Value = userData.Stat.Cash
Gems.Value = userData.Stat.Cash2

end)

If anyone can help that would be grateful of you

1 Like