Overhead GUI showing wrong player's stats

I would like this overhead GUI to show the right stats for the player, I have this function here that updates it:

function updateClient(player)
	local playerData
	while wait() do
		playerData = Data.Get(player)
		if playerData then break end;
	end
	local rap = Data.RAPCalculate(player);
	game.ReplicatedStorage["Client Updates"]:FireClient(player, playerData, rap)
	if not game.Workspace:FindFirstChild(player.Name) then return end
	if game.Workspace[player.Name].HumanoidRootPart:FindFirstChild('Overhead') then 
		game.Workspace[player.Name].HumanoidRootPart:FindFirstChild('Overhead').TextLabel.Text = 'Player > ' .. player.Name
		game.Workspace[player.Name].HumanoidRootPart:FindFirstChild('Overhead').Value.Text = "Rap > " .. addtitle(rap);
	else
		local x = script.Overhead:Clone()
		x.Parent = game.Workspace[player.Name].HumanoidRootPart
		x.TextLabel.Text = 'Player > ' .. player.Name
		x.Value.Text = "Rap > " .. addtitle(rap);
	end
end

If you’re in the server first, it won’t glitch out. But once I rejoined, it kept glitching showing 0, and then the other person’s rap aswell.

You might want to change this:

To this:

repeat
	playerData = Data.Get(player)
	task.wait()
until playerData

These do the same thing, but I personally think the second version is easier to read.

As for your actual problem at hand, are you sure the playerData received is the correct one?

Yes. I am almost 100% positive, as Data.Get returns the player data from the session Data table as shown below:

aloe.Get = function(value1)
	return sessionData[value1];
end

Have you tried printing the data out? When trouble shooting it is usually very good to add prints at the start and end of the affected functionality, and from there move the prints closer to the middle until the source is found.

Are you sure that sessionData contains the right information? You said the problem happened when you rejoined. What happens to sessionData[player] when player leaves? What happens when it joins?

When the player leaves, the session data with them stays. I’m not sure how to actually remove this without possibly erroring. When they rejoin, that data is overwritten by their datastore. The data module:

local aloe = {};
--// SERVICES \\
local DataStoreService = game:GetService("DataStoreService")

--// DATA VARS \\
local sessionData = {};
local dataSet = DataStoreService:GetDataStore("AloeV.13")
local RapSet = DataStoreService:GetOrderedDataStore('AloeV.5_Rap')
local startData = {
	Values = {
		Bux = 250000,
		Tix = 0,
		XP = 0,
		Level = 0,
		Gems = 0,
		CasesOpen = 1,
		Progress = 0,
		Prestige = 1,
	},
	Arrays = {
		Inventory = {},
		Flags = {},
		Equipped = {};
	}
}
--// FUNCTIONS \\
local function recursiveCopy(dataTable)
	local tableCopy = {}
	for index, value in pairs(dataTable) do
		if type(value) == "table" then
			value = recursiveCopy(value)
		end
		tableCopy[index] = value
	end
	return tableCopy
end
aloe.post = function(player, body, title)
	game.ReplicatedStorage.Notification:FireClient(player, body, title)
end
aloe.Get = function(value1)
	return sessionData[value1];
end
local rewards = {};
aloe.AddReward = function(player, amount)
	rewards[player] += amount
end
aloe.GetReward = function(player)
	return rewards[player]
end
aloe.RAPCalculate = function(value1)
	local inventory = sessionData[value1].Arrays.Inventory;
	if not inventory then return 0 end
	local rap = 0
	for i, v in pairs(inventory) do
		rap += v.Amount * v.Rap;
	end
	return rap;
end
aloe.StartServer = function(value1)
	if dataSet:GetAsync(value1.UserId) then
		sessionData[value1] = dataSet:GetAsync(value1.UserId)
	else
		sessionData[value1] = recursiveCopy(startData)
	end
	rewards[value1] = 0
end
aloe.Save = function(value1)
	if not sessionData[value1].Arrays.Flags then
		sessionData[value1].Arrays.Flags = {};
	end
	if not sessionData[value1].Values.Progress then
		sessionData[value1].Values.Progress = 0
	end
	dataSet:SetAsync(value1.UserId, sessionData[value1])
	RapSet:SetAsync(value1.Name .. ";" .. value1.UserId, aloe.RAPCalculate(value1))
end
aloe.GrabRap = function()
	local globalData = {};
	local page = RapSet:GetSortedAsync(false, 100);
	page = page:GetCurrentPage();

	for key, value in next, page do
		globalData[#globalData + 1] = {value.key, value.value};
	end

	return globalData;
end

aloe.Flag = function(player, FlagReason, FlagTitle)

end

aloe.GiveItem = function(player, item2, Amount)
	local item
	local inventory = sessionData[player].Arrays.Inventory;
	for i = 1, #inventory do
		local v = inventory[i]
		if v.ItemId == item2 then
			v.Amount += Amount
			return
		end
	end
	local items = require(script.Parent.Items)
	for i, v in pairs(items) do
		if v.ItemId == item2 then
			item = v
		end
	end
	item.Amount = Amount;
	inventory[#inventory + 1] = item;
end
aloe.CaseItem = function(player, item2, Amount)
	local item
	local inventory = sessionData[player].Arrays.Inventory;
	for i = 1, #inventory do
		local v = inventory[i]
		if v.ItemId == item2.ItemId then
			v.Amount += Amount
			return
		end
	end
	item2.Amount = Amount;
	inventory[#inventory + 1] = item2;
end
aloe.RemoveItem = function(player, itemId, amount)
	local inventory = sessionData[player].Arrays.Inventory;
	if not itemId then return end
	local buxToReturn = 0 -- This is only if selling and not jackpotting
	for i, v in pairs(inventory) do
		if v.ItemId == itemId then
			if v.Locked == true then
				buxToReturn = 0;
				break
			end
			if v.Amount - amount == 0 then
				buxToReturn = amount * v.Rap
				table.remove(inventory, i);
				break
			end
			if tonumber(v.Amount) > amount then
				v.Amount -= amount;
				buxToReturn = amount * v.Rap
				break
			end
		end
	end
	local x = sessionData[player].Values.Prestige
	if x == 1 then x = 1 else x = x * 5.75 end
	return buxToReturn * x
end
aloe.Prestige = function(player)
	local inventory = sessionData[player].Arrays.Inventory; -- BYPASSES LOCK
	local prestigeItemCount = 0
	for i, v in pairs(inventory) do
		if inventory[i].ItemId == 140469106 then
			prestigeItemCount += inventory[i].Amount
			break
		end
	end
	sessionData[player].Arrays.Inventory = {};
	aloe.GiveItem(player, 140469106, prestigeItemCount)
end
aloe.SellAll = function(player)
	local offset = 0;
	local inventory = sessionData[player].Arrays.Inventory;
	for index = 1, #inventory do
		local item = inventory[index - offset];

		if not item.Locked then
			if item and item.Amount > 1 then

				local averagePrice = aloe.RemoveItem(player, item.ItemId, item.Amount);

				if averagePrice then
					sessionData[player].Values.Bux = sessionData[player].Values.Bux + averagePrice;
				end
			else
				local averagePrice = aloe.RemoveItem(player, item.ItemId, item.Amount);
				if averagePrice then
					sessionData[player].Values.Bux  = sessionData[player].Values.Bux + averagePrice;
				end
			end
			offset = offset + 1;
		end
	end
end
aloe.JackpotItem = function(player, itemId, amount)
	local inventory = sessionData[player].Arrays.Inventory;
	local doesHaveItem = false
	if amount < 1 then 
		return false
	end
	for i, v in pairs(inventory) do
		if v.ItemId == itemId then
			if v.Locked == true then
				doesHaveItem = false;
				break
			end
			if v.Amount - amount == 0 then
				doesHaveItem = true;
				table.remove(inventory, i);
				break
			end
			if tonumber(v.Amount) > amount then
				v.Amount -= amount;
				doesHaveItem = true;
				break
			end
		end
	end
	return doesHaveItem;
end
return aloe;

Add the following prints, it should let you find the source of the issue:
A) When a player joins, print what data is gotten about them (will also fire when they rejoin)
B) What data aloe.Get() returns
C) What data the client receives (ClientUpdates:FireClient())

This should help track if the issue is in the aloe module or on the client.

Figured it out! It was just my data module not returning the correct players data. In order to fix this, I just looped through the data table to make sure it matched the player’s data and then returned it and it fixed it.

Glad to hear! Don’t forget to mark your most recent reply as the solution

Thank you for letting me know to do that. I’m a bit new to the forum haha!

1 Like