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