I've put some comments in the code to show where the errors are occuring. Basically, the lines of code that are supposed to be saving the data into a datastore aren't actually saving the data properly. I'm not sure why, seeing as how the only difference between that, and the line that sets the default currency value (which I already know works), is that the number 10 was changed to a variable for the value. I'm not even getting any error messages in the output I also know that the line to save it should be getting run, as I did check it by putting a print both before and after it, which did print into the output. I don't know what the issue is, and I have absolutely zero clue what could be causing this issue.
local datastore = game:GetService("DataStoreService")
local playerdata = datastore:GetDataStore("PlayerCurrency")
local value = nil
game.Players.PlayerAdded:Connect(function(player)
local previousdata = playerdata:GetAsync(player.UserId)
local currency
if previousdata ~= nil then
currency = previousdata
else
currency = 10
playerdata:SetAsync(player.UserId, 10)
end
local currencyValue = Instance.new("NumberValue", player)
currencyValue.Name = "Currency"
currencyValue.Value = currency
end)
-- Problem ↓ ----------------------------------------------------------------------------------
game:BindToClose(function()
for i,player in pairs(game.Players:GetPlayers()) do
value = player.Currency.Value
playerdata:SetAsync(player.UserId, value) -- Won't Save -----------------------
end
end)
game.Players.PlayerRemoving:Connect(function(player)
value = player.Currency.Value
if Value ~= nil then
playerdata:SetAsync(player.UserId, value) -- Won't Save -----------------------
end
end)