Hello! I am currently working on an RPG game. I’m trying to get the data to save when the player leaves, but some of the values I need to save are attributes on the player’s character. Every time I attempt to do this, it always returns the character as nil. I’m not exactly sure what I should do to fix this and still achieve what I’m trying to do.
local function SaveData(player, wiped, died)
local CT = CombatTable[player.Name]
local Character = player.Character
local key = "player_" .. player.UserId
if CT.InCombat then
wiped = true
print(player.Name .. " combat logged.")
end
if wiped then
StatTable[player.UserId] = {
Lives = 3,
Health = 100,
MaxHealth = 100,
Experience = 0,
Level = 1,
Passives = {},
Odens = 0,
TalentPoints = 0,
MaxPosture = 60
}
CharacterTable[player.UserId] = {
Appearance = {},
CurrentQuests = {},
CompletedQuests = {},
Items = {},
Location = {workspace.Thing.CFrame:GetComponents()},
Class = nil,
DawnMark = nil,
Area = nil,
}
else
StatTable[player.UserId] = {
Lives = Character:GetAttribute("Lives"),
Health = Character:GetAttribute("SpecialHealth"),
MaxHealth = 100 + GetHealthBonus(Character),
Experience = Character:GetAttribute("Experience"),
Level = Character:GetAttribute("Level"),
Passives = {},
Odens = Character:GetAttribute("Odens"),
TalentPoints = Character:GetAttribute("TalentPoints"),
MaxPosture = Character:GetAttribute("MaxPosture")
}
CharacterTable[player.UserId] = {
Appearance = {},
CurrentQuests = {},
CompletedQuests = {},
Items = {},
Location = {Character.HumanoidRootPart.CFrame:GetComponents()},
Class = nil,
DawnMark = Character:GetAttribute("DawnMark"),
Area = Character:GetAttribute("Area")
}
end
if died then
CharacterTable[player.UserId].Location = {workspace.OtherThing.CFrame:GetComponents()}
end
--print("Data saving...")
local SetSuccess, errorMessage = pcall(function()
StatData:SetAsync(player.UserId, StatTable[player.UserId])
CharacterData:SetAsync(player.UserId, CharacterTable[player.UserId])
end)
if not SetSuccess then
print("Data was unable to save.")
warn(errorMessage)
else
-- print("Successfully saved data!")
end
end
This is currently my data saving function and I use it like this:
Players.PlayerRemoving:Connect(function(player)
SaveData(player)
end)
Any ideas?