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)

1 comment
I noticed on line 14 that you have the Value you want to save indexed in the get async parameters you just need to Put the key Inside and leave out the "BlahBlah.Value" so:
DataStore:GetAsync(Key)