How can I save values in GUIs(DataStore)

I made a DataStore module that saves players data, updates it and changes it. But I want to have a GUI that has the values in it. But since this is a module script I have no idea how to do it. I required the module script inside the local script. But I don’t know what else to do.

Module Script:

--Services
local playerService = game:GetService("Players")

local DataStoreService = game:GetService("DataStoreService")

local PLAYERDATA = DataStoreService:GetDataStore("PLAYERDATA")

--Tool
local ToolsFolder = game.ServerStorage:WaitForChild("ToolsFolder")


local DATASTORE = {}

--Defining Table
local sessionData = {} -- Responsible for storing player data in a single table

--Functions

local function setupPlayerData(player)
	local playerUserId = player.UserId
	
	local data
	local success = pcall(function()
		data = PLAYERDATA:GetAsync(playerUserId)
	end)
	
	if success then
		if data then
			sessionData[playerUserId] = data
		else
			sessionData[playerUserId] = {Money = 0, Level = 0 ,Inventory = {}}
		end
		
		DATASTORE.LoadTools(player)
		
		player.CharacterAdded:Connect(function()
			DATASTORE.LoadTools(player)
		end)
		
	end
end

local function saveData(player)
	local playerUserId = player.UserId
	
	local success = pcall(function()
		PLAYERDATA:SetAsync(playerUserId,sessionData[playerUserId]) 
	end)
end


function DATASTORE.ChangeValue(player,name,value)-- If any other scripts want to access this
	local playerUserId = player.UserId
	
	if sessionData[playerUserId]then
		sessionData[playerUserId][name] = value -- For whatever value we want to set or name, we go inside storage table, access the name of the value we want to change
	end
	
end

function DATASTORE.UpdateValue(player,name,value)
	local playerUserId = player.UserId
	
	if sessionData[playerUserId] then
		sessionData[playerUserId][name] = sessionData[playerUserId][name] + value 
		
		print(sessionData[playerUserId][name])
	end
end


function DATASTORE.CreateTool(player,name)
	local playerUserId = player.UserId
	
	if not player.Backpack:FindFirstChild(name) and player.Character and not player.Character:FindFirstChild(name) then
		
		local tool = ToolsFolder:FindFirstChild(name):Clone()
		tool.Parent = player.Backpack
		
		local playerSessionData = sessionData[playerUserId]
		
		if not table.find(playerSessionData.Inventory, name) then
			table.insert(playerSessionData.Inventory, name)
		end
		
	end
end

function DATASTORE.LoadTools(player)
	
	local playerUserId = player.UserId
	
	for i, toolName in pairs(sessionData[playerUserId].Inventory) do
		print(toolName)
		DATASTORE.CreateTool(player, toolName)
	end
end

playerService.PlayerAdded:Connect(setupPlayerData)
playerService.PlayerRemoving:Connect(saveData)

return DATASTORE

local script(inside the text button)

local GAME_COMPONENTS = game.ServerStorage:FindFirstChild("GAME_COMPONENTS")

local DATASTORE = require(GAME_COMPONENTS.DATASTORE)

Hi, are you trying to access a Module Script in the ServerStorage from the client side? If so then you need to use a RemoteEvent. ServerStorage is only accessible from the server side.

Oh. Thank you for that. I was wondering why it wasn’t working when I tried getting to module script. But even after I do that, how am I going to show the values on the GUI. I tried making a function called showValueOnGUIObject. I’m on mobile right now so I can’t really show. But will that work? If not how else am I going to make the value show on the GUI

What I do is this: (From Server Side) When the player joins, check for data. If data then: send the data to the client side via a RemoteEvent:FireClient(data). Then on a local script in StarterPlayerScripts (Client Side), use RemoteEvent.OnClientEvent:Connect(function() (‘do something with the data’ end).

Is the same going to work with a Module to Client?

To send data to the module from client you would do (starting with the client side): RemoteEvent:FireServer(data), then on the server side in the module you would have a function called by OnServerEvent:Connect(data).

What I also like to do is save a table on the client side (like local data = {}, for example, and store GUI values in it), and whenever something changes on the client side, update the client table. Then when you want to save to data stores, send the table to the server via RemoteEvent:FireServer(dataTable).

It seems to show no error when I do this method. Yet there is no text on the label. Like the stats aren’t showing on the label.

Module script

--Services
local playerService = game:GetService("Players")

local DataStoreService = game:GetService("DataStoreService")

local PLAYERDATA = DataStoreService:GetDataStore("PLAYERDATA")

--Tool
local ToolsFolder = game.ServerStorage:WaitForChild("ToolsFolder")

--Remote Event
local ReplicatedStorage = game.ReplicatedStorage

local GUIEVENT = ReplicatedStorage:FindFirstChild("GUI")

local DATASTORE = {}

--Defining Table
local sessionData = {} -- Responsible for storing player data in a single table

--Functions

local function setupPlayerData(player)
	local playerUserId = player.UserId
	
	local data
	local success = pcall(function()
		data = PLAYERDATA:GetAsync(playerUserId)
	end)
	
	if success then
		if data then
			sessionData[playerUserId] = data
		else
			sessionData[playerUserId] = {Berries = 0, Level = 0 ,Bounty = 0, Inventory = {}}
		end
		
		DATASTORE.LoadTools(player)
		
		player.CharacterAdded:Connect(function()
			DATASTORE.LoadTools(player)
			**GUIEVENT.OnServerEvent:Connect(data)**
		end)
		
	end
end

local function saveData(player)
	local playerUserId = player.UserId
	
	local success = pcall(function()
		PLAYERDATA:SetAsync(playerUserId,sessionData[playerUserId]) 
	end)
end


function DATASTORE.ChangeValue(player,name,value)-- If any other scripts want to access this
	local playerUserId = player.UserId
	
	if sessionData[playerUserId]then
		sessionData[playerUserId][name] = value -- For whatever value we want to set or name, we go inside storage table, access the name of the value we want to change
	end
	
end

function DATASTORE.UpdateValue(player,name,value)
	local playerUserId = player.UserId
	
	if sessionData[playerUserId] then
		sessionData[playerUserId][name] = sessionData[playerUserId][name] + value 
		
		print(sessionData[playerUserId][name])
	end
end

function DATASTORE.CreateTool(player,name)
	local playerUserId = player.UserId
	
	if not player.Backpack:FindFirstChild(name) and player.Character and not player.Character:FindFirstChild(name) then
		
		local tool = ToolsFolder:FindFirstChild(name):Clone()
		tool.Parent = player.Backpack
		
		local playerSessionData = sessionData[playerUserId]
		
		if not table.find(playerSessionData.Inventory, name) then
			table.insert(playerSessionData.Inventory, name)
		end
		
	end
end

function DATASTORE.LoadTools(player)
	
	local playerUserId = player.UserId
	
	for i, toolName in pairs(sessionData[playerUserId].Inventory) do
		print(toolName)
		DATASTORE.CreateTool(player, toolName)
	end
end

playerService.PlayerAdded:Connect(setupPlayerData)
playerService.PlayerRemoving:Connect(saveData)


return DATASTORE

Local script


local label = script.Parent
local ReplicatedStorage = game.ReplicatedStorage
local GUIEvent = ReplicatedStorage:FindFirstChild("GUI")

local data = {}

function showStats(data)
	
	label.Text = data
	
end

GUIEvent:FireServer(data)

Your GUIEvent:FireServer(data) should be in a function, like when you want to send the data to the server to update the player’s data table on the server side.

Whenever something in game changes the text on the TextLabel, you could do:

label.Changed:Connect(function()
data.Level = label.Text
GUIEvent:FireServer(data)
end).

Now for loading to the client side, you want to do this:

GUIEvent.OnClientEvent:Connect(function(loadedData)
data = loadedData
end)

So something like this?

function DATASTORE.ChangeGuiValues(player,GuiObject,name)
	local playerUserId = player.UserId
	
	if sessionData[playerUserId] then
		GuiObject.Text = sessionData[playerUserId][name]
	end
end

I haven’t tested it but you could try that.

Thanks for the help but sadly it doesn’t work. I might just switch to using DataStores with server scripts. That seems easier to do this type of stuff.

Alrighty, good luck. Remember, FireServer(arguments) to send to server from client, OnServerEvent(player, arguments) to receive on server side from client. FireClient(player, arguments) to sent to client from server, OnClientEvent(arguments) to receive on client side from server.

I appreciate your help for all of this! Thank you.

1 Like