The problem is it isn’t firing upon getting called causing the data not to update and save.
-- services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DatastoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
--- constants
local PREFIX = "AT_BETA - "
local DATASTORE = DatastoreService:GetDataStore("Data")
local PLAYERDATA = ReplicatedStorage.PlayerData
local SAVE_TOOLS = true
local SAVE_CURRENCY = true
local SAVE_STAGE = true
-- functions
local function LoadData(player)
warn("Loading " .. player.Name .. "'s Data!")
local PlayerData = script.PlayerData:Clone()
PlayerData.Name = player.Name
local savedData
local success,error = pcall(function()
savedData = DATASTORE:GetAsync(PREFIX .. tostring(player.UserId))
end)
if success then
print("\tSuccess!")
if savedData then
if savedData.Currency then
for index,value in pairs(savedData.Currency) do
if PlayerData.Currency:FindFirstChild(index) then
PlayerData.Currency[index].Value = value
end
end
end
if savedData.Tools then
for inedex,value in pairs(savedData.Tools) do
if PlayerData.Tools:FindFirstChild(inedex) then
PlayerData.Tools[inedex].Value = value
end
end
end
if savedData.TycoonStage then
for index,value in pairs(savedData.TycoonStage) do
if PlayerData.TycoonStage:FindFirstChild(index) then
PlayerData.TycoonStage[index].Value = value
end
end
end
else
warn(string.upper(player.Name) .. " DOES NOT HAVE ANY DATA")
-- PRESET THE DATA
PlayerData.Currency.Cash.Value = 100
end
else
warn("DATA HAS ERRORED! ABORT")
local ERROR_VALUE = Instance.new("StringValue")
ERROR_VALUE.Name = "ERROR_OCCURED"
ERROR_VALUE.Value = error
ERROR_VALUE.Parent = PlayerData
end
PlayerData.Parent = PLAYERDATA
return PlayerData
end
local function SaveData(player)
local PlayerData = PLAYERDATA:FindFirstChild(player.Name)
if PlayerData then
if PlayerData:FindFirstChild("ERROR_OCCURED") then
warn("DATA UNSAFE TO SAVE! ABORTING!")
return false
end
local tries = 0
local success = false
repeat
tries += 1
warn("Try - " .. tries)
success = pcall(function()
DATASTORE:UpdateAsync(PREFIX .. tostring(player.UserId), function(data)
if data then
if not data.Currency then
data.Currency = {}
end
if not data.Tools then
data.Tools = {}
end
if not data.TycoonStage then
data.TycoonStage = {}
end
else
data = {
Currency = {},
Tools = {},
TycoonStage = {},
}
end
if SAVE_CURRENCY then
for _,value in pairs(data.Currency) do
data.Currency[value.Name] = value.Value
end
end
if SAVE_STAGE then
for _,value in pairs(data.TycoonStage) do
data.TycoonStage[value.Name] = value.Value
end
end
if SAVE_TOOLS then
for _,value in pairs(data.Tools) do
data.Tools[value.Name] = value.Value
end
end
return data
end)
end)
until success ~= false or tries == 3
return success
else
return false
end
end
script.SaveData.OnInvoke = SaveData
Players.PlayerAdded:Connect(function(player)
LoadData(player)
end)
game:BindToClose(function()
if RunService:IsStudio() then return end
coroutine.wrap(function()
for index,player in pairs(Players:GetPlayers()) do
spawn(SaveData(player))
end
end)()
end)
Players.PlayerRemoving:Connect(function(player)
SaveData(player)
if PLAYERDATA:FindFirstChild(player.Name) then
PLAYERDATA[player.Name]:Destroy()
end
end)