This is my datastore but what is the best way to implement pets?
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("PlayerSave")
local function SavePlayerData(player)
local success,errormsg = pcall(function()
local SaveData = {}
for i,stats in pairs(player.leaderstats:GetChildren()) do
SaveData[stats.Name] = stats.Value
end
SaveDataStore:SetAsync(player.UserId,SaveData)
end)
if not success then
return errormsg
end
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character) --Check if player's Character is added
player.Character.Humanoid.Died:Connect(function()
local errormsg = SavePlayerData(player)
if errormsg then
warn(errormsg)
end
end)
local Stats = Instance.new("Folder")
Stats.Name = "leaderstats"
Stats.Parent = player
local statstime = Instance.new("IntValue")
statstime.Name = "Money"
statstime.Parent = Stats
local Data = SaveDataStore:GetAsync(player.UserId)
if Data then
for i,stats in pairs(Stats:GetChildren()) do
stats.Value = Data[stats.Name]
end
else
end
end)
end)
Players.PlayerRemoving:Connect(function(player)
local errormsg = SavePlayerData(player)
if errormsg then
warn(errormsg)
end
end)
game:BindToClose(function()
for i,player in pairs(Players:GetPlayers()) do
local errormsg = SavePlayerData(player)
if errormsg then
warn(errormsg)
end
end
wait(2)
end)