Ok so i done this and have been playing with it for 2 days on and off just cant seem to see what im missing any help would be appreciated
ServerScriptService.ServerHandle.Data:107: attempt to index nil with ‘UserId’ - Server - Data:107
dataMod.set = function(player, stat, value)
local key = "Player_" .. player.UserId
sessionData [key] [stat] = value
player.leaderstats[stat].Value = value
end
Full Script
local dataMod = {}
local Players = game:GetService("Players")
local playerService = game:GetService("Players"):GetPlayerByUserId(1)
local dataService = game:GetService("DataStoreService")
local store = dataService:GetDataStore("DataStorev1")
local sessionData = {}
local dataMod = {}
dataMod.recursiveCopy = function(dataTable)
local tableCopy = {}
for index, value in pairs(dataTable) do
if type(value) == "table" then
value = dataMod.recursiveCopy(value)
end
tableCopy[index] = value
end
return tableCopy
end
local defaultData = {
Coins = 110;
Stage = 1;
}
--auto save---------------------
local AUTOSAVE_INTERVAL = 120
local function autoSave()
while wait(AUTOSAVE_INTERVAL)do
print("Auto-saving data for all players")
for key, dataTable in pairs(sessionData) do
local player = Players:GetPlayersByUserId(key)
dataMod.save(player)
end
end
end
spawn(autoSave) --Initialize autosave loop
dataMod.setupData = function(player)
local key = "Player_" .. player.UserId
local data = dataMod.load(player)
sessionData [key] = dataMod.recursiveCopy(defaultData)
if data then
for index, value in pairs(data) do
dataMod.set()
print (index,value)
dataMod.set(player, index,value)
end
print (player.Name.."`s data has been loaded!")
else
print (player.Name.."`s is a new player!")
end
end
--saveing data-----------------------------------------
game:BindToClose(function()
for _, player in pairs(Players:GetPlayers())do
dataMod.save(player)
player:kick("Shutting down game data saved.")
end
end)
Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name= "leaderstats"
folder.Parent= player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = folder
coins.Value = defaultData.Coins
local stage = Instance.new ("IntValue")
stage.Name = "Stage"
stage.Parent = folder
stage.Value = defaultData.Stage
dataMod.setupData(player)
end)
dataMod.set = function(player, stat, value)
local key = "Player_" .. player.UserId
sessionData [key] [stat] = value
player.leaderstats[stat].Value = value
end
dataMod.increment = function (player, stat, value)
local key = "Player_" .. player.UserId
sessionData[key][stat] = dataMod.get(player, stat)+ value
end
dataMod.get = function(player, stat)
local key = "Player_" .. player.UserId
return sessionData[key][stat]
end
dataMod.save = function(player)
local key = "Player_" .. player.UserId
local data = dataMod.recursiveCopy(sessionData[key])
local success, err = pcall(function()
store:SetAsync (key, data)
end)
if success then
print(player.Name.."`s data has been saved!")
end
end
dataMod.load = function(player)
local key = "Player_" .. player.UserId
local data
local success, err = pcall(function()
data = store:GetAsync (key)
end)
if not success then
dataMod.load(player)
end
print(player.Name.."`data has been loaded")
return data
end
--removes player--------------------------------
dataMod.removeSessionData = function(player)
local key = "Player_" .. player.UserId
sessionData[key] = nil
end
Players.PlayerRemoving:Connect (function(player)
dataMod.save(player)
dataMod.removeSessionData(player)
end)
return dataMod