Problem with Passing Tables

So currently, I’m working to make a functional player’s stats.
And then I encountered a pretty weird problem.
So my idea was to make a script to detect whenever a player dies, the script will set their inBattle state to false. However, when I was trying to pass the player’s stats table from scripts to scripts, it’s appear the table to be nil.
Hope you guys will spend a bit of time looking at this topic ,thank you!

— Script that set player’s inBattle state to false —

local RS = game:GetService("ReplicatedStorage")
local Players = game.Players
local PlayerStats = require(RS.GameDataStorage.PlayerStats)
local SSS = game:GetService("ServerScriptService")
local LPS = require(SSS.Setup.LocalPlayerStats)

local function LocalStat(player)

	local UserID = game:GetService("Players"):GetUserIdFromNameAsync(player.Name)
	
	local stat = LPS:GetPlayerStats(UserID)
	print(stat)
	return stat

end
Players.PlayerAdded:Connect(function(plr)
	
	local UID = game:GetService("Players"):GetUserIdFromNameAsync(plr.Name)
	local stat = LocalStat(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local humanoid = char:WaitForChild("Humanoid")
	
	humanoid.Died:Connect(function()
		
		print("died1")
		stat.inBattle = false
		
	end)
	
end)

— Script that passes the table of a player —

local RS = game:GetService("ReplicatedStorage")
local Players = game.Players
local PlayerStats = require(RS.GameDataStorage.PlayerStats)
local plr = {}
local player = {}
Players.PlayerAdded:Connect(function(plr)
	local UserId = Players:GetUserIdFromNameAsync(plr.Name)
	player[UserId] = PlayerStats:new(UserId)
end)
function plr:GetPlayerStats (UID)

	for i,v in pairs(player) do
		if v.UserID == UID then
			return v
		end
	end
end
return plr

— Script that create new stats —

local PlayerStats = {}
PlayerStats.__index = PlayerStats
function PlayerStats:new(userid)
	
	local stat = setmetatable({},self)
	
	stat.ID = 0
	stat.CD = false
	stat.Cash = 0
	stat.Blocking = false
	stat.currentCombo = 0
	stat.UserID = userid
	stat.inBattle = false
	return stat
	
end

function PlayerStats:update(new_id,CD,cash_amount,is_blocking,current_combo,is_inBatlle)
	
	self.ID = new_id or self.ID
	self.CD = CD or self.CD
	self.Cash = cash_amount or self.Cash
	self.Blocking = is_blocking or self.Blocking
	self.currentCombo = current_combo or self.currentCombo
	self.inBattle = is_inBatlle or self.inBattle
	
end

return PlayerStats	

Hey there, code looks ok. My only critique as of a quick look is using a loop to get the player ID. I would prefer a dictionary since there is a unique key value possible (But tbh performance impact is pretty minimal).

Otherwise it seems like a load order problem there are two player added connections and you cannot exactly guarantee one connection will run before another. I would perform another print check within the player added connections to confirm the run/line order.

Perhaps try moving your stat down to the died connection to ensure it runs after the setup in the module script:

local RS = game:GetService("ReplicatedStorage")
local Players = game.Players
local PlayerStats = require(RS.GameDataStorage.PlayerStats)
local SSS = game:GetService("ServerScriptService")
local LPS = require(SSS.Setup.LocalPlayerStats)

local function LocalStat(player)

	local UserID = game:GetService("Players"):GetUserIdFromNameAsync(player.Name)
	
	local stat = LPS:GetPlayerStats(UserID)
	print(stat)
	return stat

end
Players.PlayerAdded:Connect(function(plr)
	
	local char = plr.Character or plr.CharacterAdded:Wait()
	local humanoid = char:WaitForChild("Humanoid")
	
	humanoid.Died:Connect(function()
			local UID = game:GetService("Players"):GetUserIdFromNameAsync(plr.Name)
	local stat = LocalStat(plr)
		print("died1")
		stat.inBattle = false
		
	end)
	
end)
1 Like

Thank you for helping me out ! It worked well !
There was a problem that the script worked once only so I add the CharacterAdded event and it was fixed
Anyway, thank you so much!

1 Like

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