I've stayed up all night trying to get this to work but it just wouldn't . The code makes sense to me, so I can't figure out why it won't work. The output says no errors, just returning a nil value for trying to find the player Id. Which means it is not properly saving my Id to the data bank.
Heres the code. I've labeled it. The code is in a regular script inside 'ServerScriptService'
--Creates the data Store as ds local ds = game:GetService("DataStoreService"):GetDataStore("-xBuckDatax-") game.Players.PlayerAdded:Connect(function(player) -- saves the player Id as key local key = "Player_"..player.UserId local folder = Instance.new("Folder",player) folder.Name = "leaderstats" local money = Instance.new("IntValue",folder) money.Name = 'ObbyBucks<|>' local moneyalready --moneyalready is the id of the player local success, err = pcall(function() moneyalready.Value = ds:GetAsync(key) end) -- if it found the players Id, It made money equivilent to what was saved in the data store if success then money.Value = moneyalready else --If not, money is equivilent to 100 and it saves that to the data store money.Value = 100 ds:SetAsync(key, 100) end end) -- when someone is leaving... game.Players.PlayerRemoving:Connect(function(player) local key = "Player_"..player.UserId --saves their key and money to the data store ds:SetAsync(key, player.leaderstats["ObbyBucks<|>"].Value) end)
You should save using tables. This will make it easier in the future to add other values too. I fixed up your code below but keep in mind this is untested.
local Players = game:GetService("Players") local DataStoreService = game:GetService("DataStoreService") local DataStore = DataStoreService:GetDataStore("-xBuckDatax-") -- Making the saving inside a function it shortened your code by a bit. local function Save(player) local key = "Player_"..player.UserId local save = { ["ObbyBucks"] = player.leaderstats["ObbyBucks<|>"].Value } --saves their key and money to the data store local success, err = pcall(function() DataStore:SetAsync(key, save) end) if not success then warn("Failed to over-write data"..tostring(err)) return end end -- I made this a function so it can be called whenever local function Load(player) -- saves the player Id as key local key = player.UserId local folder = Instance.new("Folder") folder.Name = "leaderstats" folder.Parent = player -- setting the parent in the .new is deprecated local money = Instance.new("IntValue") money.Name = "ObbyBucks<|>" money.Parent = folder local moneyAlready local success, err = pcall(function() moneyAlready.Value = DataStore:GetAsync(key) end) if not success then warn("Failed to read data"..tostring(err)) return end if moneyAlready then money.Value = moneyAlready.ObbyBucks else Save(player) end end Players.PlayerAdded:Connect(Load) Players.PlayerRemoving:Connect(Save)
All I did was improve the code.
What I did:
Split the DataStore
variables, made the saving and loading in a function that you control when it's called, editing the save and load system, removed deprecated code.