it loads but nothing saves when I'm in a server
local main = require(game.ServerScriptService.Main) game.Players.PlayerAdded:Connect(function(player) main.GiveStats(player) end) game.Players.PlayerRemoving:Connect(function(player) main.SaveStats(player) end)
Module script:
function Main.GiveStats(player) local datastore = game:GetService("DataStoreService") local leaderstats = Instance.new("IntValue") leaderstats.Name = "leaderstats" leaderstats.Parent = player local Ores = Instance.new("Folder", player) Ores.Name = "Ores" local points = Instance.new("IntValue", leaderstats) points.Name = "Cash" local Pickaxes = Instance.new("Folder", player) Pickaxes.Name = "Pickaxes" local key = player.userId local Blacklist = Instance.new("Folder", player) Blacklist.Name = "Blacklist" local ls = datastore:GetOrderedDataStore("CashSaveSystem"):GetAsync(key) for i = 1, #Main.Ores do if datastore:GetOrderedDataStore(Main.Ores[i]):GetAsync(key) then local Item = Instance.new("IntValue", Ores) Item.Name = Main.Ores[i] Item.Value = datastore:GetOrderedDataStore(Main.Ores[i]):GetAsync(key) end end if ls then points.Value = ls end Main.GivePlayerTycoon(player) end function Main.SaveStats(plr) local key = plr.UserId local ds = game:GetService("DataStoreService") for i,v in pairs(plr.Ores:GetChildren()) do ds:GetOrderedDataStore(v.Name):SetAsync(key, v.Value) end local ls = plr.leaderstats.Cash.Value ds:SetAsync(key, ls) end
key
variable is an int64
of the player's UserId
. The key needs to be a string. Also, on line 34, you need to save the value as a table. Here's your fixed code:local Main = {} function Main.GiveStats(player) local datastore = game:GetService("DataStoreService") local leaderstats = Instance.new("IntValue") leaderstats.Name = "leaderstats" leaderstats.Parent = player local Ores = Instance.new("Folder") Ores.Name = "Ores" Ores.Parent = player -- Parent argument to instance.new is deprecated, parent in another line local points = Instance.new("IntValue") points.Name = "Cash" pointe.Parent = leaderstats local Pickaxes = Instance.new("Folder") Pickaxes.Name = "Pickaxes" Pickaxes.Parent = player local key = "Player_".. player.UserId local Blacklist = Instance.new("Folder") Blacklist.Name = "Blacklist" Blacklist.Parent = player local ls = datastore:GetOrderedDataStore("CashSaveSystem"):GetAsync(key) for i = 1, #Main.Ores do local oresData = datastore:GetOrderedDataStore(Main.Ores[i]):GetAsync(key) if oresData then local Item = Instance.new("IntValue", Ores) Item.Name = Main.Ores[i] Item.Value = oresData[1] end end if ls then points.Value = ls[1] -- need to index like this end Main.GivePlayerTycoon(player) end function Main.SaveStats(plr) local key = "Player_".. plr.UserId -- needs to be a string. my key is an example. local ds = game:GetService("DataStoreService") for i,v in pairs(plr.Ores:GetChildren()) do ds:GetOrderedDataStore(v.Name):SetAsync(key, {v.Value}) end local ls = plr.leaderstats.Cash.Value ds:SetAsync(key, {ls}) -- the saving problem was here. make it a table end return Main