Hello!
This is my first time working with datastores. Right now as the title suggests my datastore script is not saving information properly. This script is supposed to be saving multiple attributes of the player who fired the remote event’s character. I checked and the information is being assigned to the lua newcharacter
correctly, but it is not being saved. However I am unable to find why it is not saving the information. Please help me!
(This is also my first time posting on the roblox dev forum, so I apologize for any mistakes I have in formatting)
saving part of datastore script:
function CharacterSaved(player,saveFile)
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
print(newcharacter.character1.shirtId)
print("works")
print(saveFile)
local success, newcharacter = 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")
end
else
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 success, newcharacter = 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)