I don’t know much about datastores and how the data is accessed, I just got the system working where I added to a value. But I am trying to make a TextLabel display how much I have but I cannot seem to get the data from the Datastore script into the StarterGui local script. Datastore Code -
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DataTemplate = require(script.DataTemplate)
local GetDataBindable = ReplicatedStorage.Remotes:WaitForChild("GetData")
local UpdateDataBindable = ReplicatedStorage.Remotes:WaitForChild("UpdateData")
local DataVersion = script:GetAttribute("DataVersion")
local DataStore = DataStoreService:GetDataStore("PlayerStore" .. tostring(DataVersion))
local KeyPrefix = "PlayerData-"
local LoadedData = {}
local function WaitForRequestBudget(RequestType)
local CurrentBudget = DataStoreService:GetRequestBudgetForRequestType(RequestType)
while CurrentBudget < 1 do
CurrentBudget = DataStoreService:GetRequestBudgetForRequestType(RequestType)
task.wait(5)
end
end
local function GetData(Player)
local Data = LoadedData[Player]
if not Data then return end
return Data
end
local function Save(Player, OnShutdown)
local Key = KeyPrefix .. tostring(Player.UserId)
local Success, Error
local Data = GetData(Player)
if not Data then return end
repeat
if not OnShutdown then WaitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync) end
Success, Error = pcall(function()
DataStore:UpdateAsync(Key, function(PreviousData)
return Data
end)
end)
until Success
if not Success then
warn("failed to save data" .. tostring(Error))
return false
end
return true
end
local function OnLeave(Player)
Save(Player)
LoadedData[Player] = nil
return true
end
local function Load(Player)
local Key = KeyPrefix .. tostring(Player.UserId)
local Data
local Success, Error
repeat
WaitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
Success, Error = pcall(function()
Data = DataStore:GetAsync(Key)
end)
until Success or Players:FindFirstChild(tostring(Player))
if not Success then
warn("failed to load data" .. tostring(Error))
Player:Kick("Failed to load data. Please Rejoin.")
return false
end
if not Data then Data = DataTemplate end
LoadedData[Player] = Data
return true
end
local function OnGameClose()
if RunService:IsStudio() then task.wait(2); return end
local AllPlayersSaved = Instance.new("BindableEvent")
local AllPlayers = Players:GetPlayers()
local RemainingPlayers = #AllPlayers
for index, Player in ipairs(AllPlayers) do
task.spawn(function()
Save(Player, true)
RemainingPlayers -= 1
if RemainingPlayers < 1 then AllPlayersSaved:Fire() end
end)
end
AllPlayersSaved.Event:Wait()
end
local function UpdateData(Player, Key, Value)
local Data = GetData(Player)
if not Data then return end
Data[Key] = (Data[Key] or 0) + Value
print(Data.trash)
end
Players.PlayerAdded:Connect(Load)
Players.PlayerRemoving:Connect(OnLeave)
GetDataBindable.Event:Connect(function(Player)
return GetData(Player)
end)
UpdateDataBindable.Event:Connect(function(Player, Key, Value)
UpdateData(Player, Key, Value)
end)
game:BindToClose(OnGameClose)