Error trying to save data in dataStore
The moment I enter the game and exit it I get a dataStore error saying ServerScriptService.PlayerData:82: attempt to index nil with number. But I made sure that if the player starts and has no money or pets, assign a default value, then update the player’s money as Cash.Value changes and when closing the game save the pets. What I don’t understand is why once all this is done the values are not saved and are reset. Does anyone have a solution? Thank you
local httpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local dataStoreService = game:GetService("DataStoreService")
local dataBase = dataStoreService:GetDataStore("MyDataStore")
local Players = game:GetService("Players")
local RS = game:GetService("RunService")
local DataObject = {0, {}}
function clonAndMovePet(player, petName)
local petsFolder = ReplicatedStorage:FindFirstChild("PetsFolder")
if not petsFolder then return end
local petModel = petsFolder:FindFirstChild(petName)
if not petModel then return end
local petsPlayerFolder = workspace:FindFirstChild("PlayerPets")
if not petsPlayerFolder then
petsPlayerFolder = Instance.new("Folder")
petsPlayerFolder.Name = "PlayerPets"
petsPlayerFolder.Parent = workspace
end
local playerFolder = petsPlayerFolder:FindFirstChild(player.Name)
if not playerFolder then
playerFolder = Instance.new("Folder")
playerFolder.Name = player.Name
playerFolder.Parent = petsPlayerFolder
end
local clonedPet = petModel:Clone()
clonedPet.Parent = playerFolder
end
function PlayerAdded(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local CashValue = Instance.new("IntValue", leaderstats)
CashValue.Name = "CashValue"
local petInventoryFolder = Instance.new("Folder", player)
petInventoryFolder.Name = "PetInventoryFolder"
local attempt = 1
local success = nil
local playerData = nil
repeat
success, playerData = pcall(function()
DataObject = dataBase:GetAsync(player.UserId)
end)
attempt += 1
if not success then
warn(playerData)
task.wait(3)
end
until success or attempt == 5
if not DataObject then
DataObject = {0, {}}
end
if DataObject[1] ~= nil then
CashValue.Value = DataObject[1]
end
CashValue.Changed:Connect(function()
DataObject[1] = CashValue.Value
end)
if DataObject[2] ~= nil then
local petData = DataObject[2]
for petName, petValue in pairs(petData) do
local petFolder = Instance.new("Folder", petInventoryFolder)
petFolder.Name = petName
local petValueObj = Instance.new("StringValue", petFolder)
petValueObj.Name = "ID"
petValueObj.Value = petValue
end
end
if DataObject then
print(DataObject[2])
if DataObject[2] then
for petID, petName in pairs(DataObject[2]) do
clonAndMovePet(player, petName)
end
elseif DataObject[2] == nil then
DataObject[2] = {}
end
else
print("DataObject es nulo en PlayerAdded")
end
end
function PlayerRemoving(player)
local petInventoryFolder = player:FindFirstChild("PetInventoryFolder")
local petData = {}
for i, petFolder in pairs(petInventoryFolder:GetChildren()) do
for i, petValue in pairs(petFolder:GetChildren()) do
if petValue.Name == "ID" then
petData[petFolder.Name] = petValue.Value
end
end
end
if DataObject then
DataObject[2] = petData
else
print("No data object")
end
if DataObject then
local attempt = 1
local success = nil
local errorMsg = nil
repeat
success, errorMsg = pcall(function()
dataBase:SetAsync(player.UserId, DataObject)
end)
attempt += 1
until success or attempt == 5
if success then
print("Data saved successfully for", player.Name)
else
warn("Error saving data", errorMsg)
end
else
print("No data object")
end
print(DataObject)
end
function ServerShutDown()
if RS:IsStudio() then
return
end
task.spawn(function()
PlayerRemoving(Players:GetPlayers())
end)
end
game:BindToClose(ServerShutDown)
game.Players.PlayerRemoving:Connect(PlayerRemoving)
game.Players.PlayerAdded:Connect(PlayerAdded)