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)

playersLeft = playersLeft + 1

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local bucks = Instance.new("IntValue")
bucks.Name = "bucks"
bucks.Parent = leaderstats
bucks.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

end)

local bindableEvent = Instance.new("BindableEvent")

game.Players.PlayerRemoving:Connect(function(player)

pcall(function()

    dataStores:SetAsync(player.UserId.."-bucks",player.leaderstats.bucks.Value)

    print("Saved")

    playersLeft = playersLeft - 1

    bindableEvent:Fire()
end)

end)

game:BindToClose(function()

while playersLeft > 0 do

    bindableEvent.Event:Wait()
end

end)

0
Please reformat your code. Thetacah 712 — 5y

Answer this question