Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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)

01playersLeft = playersLeft + 1
02 
03local leaderstats = Instance.new("Folder")
04leaderstats.Name = "leaderstats"
05leaderstats.Parent = player
06 
07local bucks = Instance.new("IntValue")
08bucks.Name = "bucks"
09bucks.Parent = leaderstats
10bucks.Value = 0

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)

01pcall(function()
02 
03    dataStores:SetAsync(player.UserId.."-bucks",player.leaderstats.bucks.Value)
04 
05    print("Saved")
06 
07    playersLeft = playersLeft - 1
08 
09    bindableEvent:Fire()
10end)

end)

game:BindToClose(function()

1while playersLeft > 0 do
2 
3    bindableEvent.Event:Wait()
4end

end)

0
Please reformat your code. Thetacah 712 — 5y

Answer this question