How can I display cash value on a GUI with with DataModule?

I have this DataModule which saves player data when they leave or creates brand new data if the player is new to the game but I was wondering how can I display the cash data from the DataModule on a GUI?

Module:

local DataStoreService = game:GetService("DataStoreService")

local PlayerDataStore = DataStoreService:GetDataStore("PlayerDataStore")

local DataModule = {}

local SessionData = {}

function DataModule.SetupPlayerData(Player)
	local Data
	
	local Success, ErrorMessage = pcall(function()
		Data = PlayerDataStore:GetAsync(Player.UserId)
	end)
	
	if Success then
		if Data then
			SessionData[Player.UserId] = Data
			
			for _, Value in pairs(SessionData[Player.UserId]) do
				print(Value)
			end
		else
			SessionData[Player.UserId] = {
				Inventory = {},
				Level = 0,
				Exp = 0,
				Cash = 0,
				Gems = 0
			}
		end
	else
		warn(ErrorMessage)
		
		Player:Kick("Failed to setup your data. Please rejoin.")
	end
end

function DataModule.SavePlayerData(Player)
	local Success, ErrorMessage = pcall(function()
		PlayerDataStore:SetAsync(Player.UserId, SessionData[Player.UserId])
	end)
	
	if Success then
		print("Data saved successfully for:", Player.Name)
	else
		warn("Data error for:", Player.Name, ErrorMessage)
	end
end

function DataModule.UpdateValue(Player, CurrencyName, CurrencyValue)
	if SessionData[Player.UserId] then
		SessionData[Player.UserId][CurrencyName] += CurrencyValue
	end	
end

return DataModule

You can Either Access the ModuleScript and get the Players Session Data, or Create a Value, Update that Value Via Table Data and vice versa, and get that Said Value and Paste it onto a UI

Also for the pcall, you can just do this:

local Success, Data = pcall(function()
	return PlayerDataStore:GetAsync(Player.UserId)
end)

if Success then
   if Data then
1 Like

Just realized returning that would have done the same lol. I put the modulescript into replicatedstorage so the client and server can access it but when I go to access it in the client script it just gives me the functions inside the module to call. How can I access that table inside it from the client script?

ModuleScript:

local DataModule = {}

DataModule.Service = game:GetService("DataStoreService")
DataModule.Store = DataModule.Service:GetDataStore("PlayerDataStore")
DataModule.SessionData = {}

LocalScript:

local DataStore = require(game.ReplicatedStorage.DataStore) -- whatever its called

TextLabel.Text = DataStore.SessionData[Player.UserId].Item

Off topic but, Thanks for the 200th Solution!

1 Like

That’s ok lol. Thanks for the help lately :slight_smile:

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