Need help editing and receiving data from datastore

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)
1 Like

well I can’t really read your script like this, but you can send data from the server to the client through remote events or by setting a value for it to replicate

please use ``` to surround all the code

So I could make a folder under the Player with the values from the DataTemplate and edit them for the client to read?

let’s say there’s a number on the server script that you want the client to have. on the server script, you create a NumberValue and parent it to player (or a folder inside of player)
and set NumberValue.Value to the number. then, on the client, you receive the number by doing player.NumberValue.Value

also you can use attributes, same thing

I just did this and it works great, thank you very much.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.