Hello!
I am trying to create a datastore that saves and loads the player’s character. However, some of the values are not saved in the CharacterDataStore:SetAsync
part of the datastore. I do not know how to fix this, as it is printing the information correctly, however when you attempt to load your character the information isn’t there, and is printing nil. I have left comments throughout the code detailing what is happening a bit. Please help me fix this!
(This is a repost as I did not get an answer on my original post, and it is against the rules for you to bump your own thread as it is considered spamming)
local DataStoreService = game:GetService("DataStoreService")
local CharacterDataStore = DataStoreService:GetDataStore("CharacterDataStore")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SaveCharacter = ReplicatedStorage:WaitForChild("SaveCharacterEvent")
local LoadCharacter = ReplicatedStorage:WaitForChild("LoadCharacterEvent")
function DatastoreLoad(player,saveFile) -- Changing player's character
print(saveFile)
local playerKey = "Player_" .. player.UserId
local newcharacter
local success = pcall(function()
--getting datastore
newcharacter = CharacterDataStore:GetAsync(playerKey)
print(playerKey)
end)
if success then
print("itworks")
if newcharacter == nil then
print("character is equal to nill")
print(newcharacter)
else
if newcharacter.character1 == nil then
print("newcharacter.character1 is equal to nil")
else
print(newcharacter.character1.shirtId)
local CharacterChange
if saveFile == 1 then
CharacterChange = newcharacter.character1
end
if saveFile == 2 then
CharacterChange = newcharacter.character2
end
if saveFile == 3 then
CharacterChange = newcharacter.character3
end
if saveFile == 4 then
CharacterChange = newcharacter.character4
end
if CharacterChange == nil then
print("failed")
local Fail = nil
LoadCharacter:FireClient(player,Fail,saveFile)
else
--prints acessories, but there is no shirtId or anything else besides acessories.
-- I haven't done anything to change the character yet, as i am trying to get the values correct.
print("charcterbeingchanged")
local acessories = newcharacter.character1[1]
local acessory = acessories[1]
print(acessory[1])
print(newcharacter.character1[0])
--prints nil
print(newcharacter.character1.shirtId)
--prints nill
LoadCharacter:FireClient(player,saveFile)
end
end
end
else
print("no work")
end
end
LoadCharacter.OnServerEvent:Connect(DatastoreLoad)
function CharacterSaved(player,saveFile) -- Saving character onto datastore
local newcharacter = {
character1 = 0,
character2 = 0,
character3 = 0,
character4 = 0,
}
local playerKey = "Player_" .. player.UserId
local character = player.Character
local success, newcharacter = pcall(function()
newcharacter = CharacterDataStore:GetAsync(playerKey)
print(playerKey)
end)
if success then
print("work")
if newcharacter == nil then
local children = player.Character:GetChildren()
local Acessories = {
}
local key = 0
for i, child in ipairs(children) do
if child:IsA("Accessory") then
local meshID = child.Handle:FindFirstChildWhichIsA("FileMesh").MeshId
local TextureID = child.Handle:FindFirstChildWhichIsA("FileMesh").TextureId
key = key + 1
Acessories[key] = { meshID,TextureID}
end
end
local character = {
shirtId = player.Character.Shirt.ShirtTemplate,
pantsId = player.Character.Pants.PantsTemplate,
bodycolorId = player.Character:FindFirstChild("Body Colors").HeadColor,
faceId = player.Character.Head.face.Texture,
Acessories
}
if saveFile == 1 then
newcharacter = {
character1 = character,
}
end
if saveFile == 2 then
newcharacter = {
character2 = character,
}
end
if saveFile == 3 then
newcharacter = {
character3 = character,
}
end
if saveFile == 4 then
newcharacter = {
character4 = character,
}
end
-- prints all of the values correctly
print(character.shirtId)
print(character.pantsId)
print(newcharacter.character1.shirtId)
print(newcharacter.character1.pantsId)
print(newcharacter.character1.faceId)
-- prints correctly here
print("works")
print(saveFile)
local success = pcall(function()
--saving character
CharacterDataStore:SetAsync(playerKey, newcharacter)
end)
if not success then
print("character was not saved")
end
if success then
--prints these correctly
print("Character was saved sucessfully")
print(newcharacter.character1.shirtId)
end
else -- ignore things past this, not needed for right now.
local children = player.Character:GetChildren()
local Acessories = {
}
local key = 0
for i, child in ipairs(children) do
if child:IsA("Accessory") then
local meshID = child.Handle:FindFirstChildWhichIsA("FileMesh").MeshId
local TextureID = child.Handle:FindFirstChildWhichIsA("FileMesh").TextureId
key = key + 1
Acessories[key] = { meshID,TextureID}
end
end
local character = {
shirtId = player.Character.Shirt.ShirtTemplate,
pantsId = player.Character.Pants.PantsTemplate,
bodycolorId = player.Character:FindFirstChild("Body Colors"),
faceId = player.Character.Head.Face.Texture,
Acessories
}
print(saveFile)
newcharacter[saveFile] = character
local succes = pcall(function()
CharacterDataStore:SetAsync(playerKey, newcharacter)
end)
if not success then
print("character was not saved")
end
if success then
print("Character was saved sucessfully")
print(character[1])
print(character[2])
print(character[3])
print(character[4])
end
end
end
end
SaveCharacter.OnServerEvent:Connect(CharacterSaved)