Why is my data store not loading or saving?
Asked by
5 years ago Edited 5 years ago
``local dataStores = game:GetService("DataStoreService"):GetDataStore("Data1")
local defaultCash = 10
local playersLeft = 0
game.Players.PlayerAdded:Connect(function(player)
01 | playersLeft = playersLeft + 1 |
03 | local leaderstats = Instance.new( "Folder" ) |
04 | leaderstats.Name = "leaderstats" |
05 | leaderstats.Parent = player |
07 | local bucks = Instance.new( "IntValue" ) |
09 | bucks.Parent = leaderstats |
local player_data
pcall(function()
player_data = dataStores:GetAsync(player.UserId.."-Bucks")
end)
if player_data ~= nil then
bucks.Value = player_data
else
bucks.Value = defaultCash
end)
local bindableEvent = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(player)
03 | dataStores:SetAsync(player.UserId.. "-bucks" ,player.leaderstats.bucks.Value) |
07 | playersLeft = playersLeft - 1 |
end)
game:BindToClose(function()
1 | while playersLeft > 0 do |
3 | bindableEvent.Event:Wait() |
end)