I am just beginning to learn about datastores but the one I modified below does not save the player's money. I do not get any errors in the output. What is the error?
local MoneyBindableEvent = game:GetService("ReplicatedStorage").Events:WaitForChild("AddMoney")
local array = {}
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local points = Instance.new("IntValue", leaderstats)
points.Name = "Money"
points.Value = game:GetService("DataStoreService"):GetDataStore("Money"):GetAsync("user_" ..player.userId)
array[player.Name] = points.Value
end)
MoneyBindableEvent.Event:Connect(function(price, plr)
print("Recieved")
local player = game.Players:WaitForChild(plr)
player.leaderstats.Money.Value = player.leaderstats.Money.Value + price
print("Price is $"..price)
print("Player is:"..plr)
end)
game.Players.PlayerRemoving:Connect(function(player)
if array[player.Name] ~= nil then
game:GetService("DataStoreService"):GetDataStore("Money"):UpdateAsync("user_"..player.userId, function(oldValue)
local newValue = array[player.Name]
local val = newValue
return newValue
end)
wait()
array[player.Name] = nil
end
end)

2 comments
You could just use something like local dataStoreService = game:GetService("DataStoreService") and you could also use local moneyData = dataStoreService:GetDataStore("Money")
Also, on line 19, a player may be new and not have any data so you should put "or 0" after the GetAsync()