Can someone tell me what I can do to make this leaderstats better?

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("YumsSave")
local ds2 = DataStore:GetDataStore("BurgerSave")
local ds3 = DataStore:GetDataStore("Rebirthsave")
local ds4 = DataStore:GetDataStore("Petvaluesave")
local ds5 = DataStore:GetDataStore("PetSave")
local ds6 = DataStore:GetDataStore("PetSavevalue")
local ds7 = DataStore:GetDataStore("PetSavevalue2")
local ds8 = DataStore:GetDataStore("PetSavevalue3")
local ds9 = DataStore:GetDataStore("PetSavevalue4")
local ds10 = DataStore:GetDataStore("PetSavevalue5")
local ds11 = DataStore:GetDataStore("PetSavevalue6")
local try = 0
local waitt = 0



game.Players.PlayerAdded:connect(function(player)

	local playerkey = player.UserId
	print("stats")
local leader = Instance.new("Folder")
leader.Name = "leaderstats"
leader.Parent = player  
	
local pet = Instance.new("NumberValue")
	pet.Name = "petvalue"
	pet.Parent = player
	pet.Value = 0
	print("pet value loaded")
	
local Yums = Instance.new("IntValue") 
	Yums.Name = "Yums"
	Yums.Parent = leader
	local w4, l4 = pcall(function()
		Yums.Value = ds1:GetAsync(player.UserId)
		ds1:SetAsync(player.UserId, Yums.Value)
	end)
	print("Yums loaded")
	print(script.Parent)

local burger = Instance.new("IntValue") 
	burger.Name = "burger"
	burger.Parent = leader
	local w3, l3 = pcall(function()
		burger.Value = ds2:GetAsync(player.UserId)
		ds2:SetAsync(player.UserId, burger.Value)
	end)
	print("burger level loaded")
	
local rebirth = Instance.new("IntValue")
	rebirth.Name = "Rebirth"
	rebirth.Parent = leader
	local w5, l5 = pcall(function()
		rebirth.Value = ds3:GetAsync(player.UserId)
		ds3:SetAsync(player.UserId, rebirth.Value)
	end)
	print("rebirths")
	
	local pets = Instance.new("Folder")
	pets.Name = "pets"
	pets.Parent = leader
	
	local petone = Instance.new("IntValue")
	petone.Parent = pets
	petone.Name = "one"
	local w6, l6 = pcall(function()
		petone.Value = ds6:GetAsync(player.UserId)
		ds6:SetAsync(player.UserId, petone.Value)
	end)
	print("pet1 loaded")
	
	local pettwo = Instance.new("IntValue")
	pettwo.Parent = pets
	pettwo.Name = "two"
	local w7, l7 = pcall(function()
		pettwo.Value = ds7:GetAsync(player.UserId)
		ds7:SetAsync(player.UserId, pettwo.Value)
	end)
	print("pet2 loaded")
	
	local petthree = Instance.new("IntValue")
	petthree.Parent = pets
	petthree.Name = "three"
	local w8, l8 = pcall(function()
		petthree.Value = ds8:GetAsync(player.UserId)
		ds8:SetAsync(player.UserId, petthree.Value)
	end)
	print("pet3 loaded")
	
	local petfour = Instance.new("IntValue")
	petfour.Parent = pets
	petfour.Name = "four"
	local w9, l9 = pcall(function()
		petfour.Value = ds9:GetAsync(player.UserId)
		ds9:SetAsync(player.UserId, petfour.Value)
	end)
	print("pet4 loaded")
	
	local petfive = Instance.new("IntValue")
	petfive.Parent = pets
	petfive.Name = "five"
	local w10, l10 = pcall(function()
		petfive.Value = ds10:GetAsync(player.UserId)
		ds10:SetAsync(player.UserId, petfive.Value)
	end)
	print("pet5 loaded")
	
	local petsix = Instance.new("IntValue")
	petsix.Parent = pets
	petsix.Name = "six"
	local w11, l11 = pcall(function()
		petsix.Value = ds11:GetAsync(player.UserId)
		ds11:SetAsync(player.UserId, petsix.Value)
	end)
	print("pet6 loaded")
	
	if w3 then
		print("load succes")
	end
	if l3 then
		warn(l3)
	end	
	if w4 then
		print("load succes")
	end
	if l4 then
		warn(l4)
	end	
	if w5 then
		print("load succes")
	end
	if l5 then
		warn(l5)
	end	
	if w6 then
		print("load succes")
	end
	if l6 then
		warn(l6)
	end	
	if w7 then
		print("load succes")
	end
	if l7 then
		warn(l7)
	end	
	if w8 then
		print("load succes")
	end
	if l8 then
		warn(l8)
	end	
	if w9 then
		print("load succes")
	end
	if l9 then
		warn(l9)
	end	
	if w10 then
		print("load succes")
	end
	if l10 then
		warn(l10)
	end	
	if w11 then
		print("load succes")
	end
	if l11 then
		warn(l11)
	end	
	
	
	petone.Changed:Connect(function()
		local w, l = pcall(function()
			ds6:UpdateAsync(playerkey, function(oldvalue)
				return player.leaderstats.pets.one.Value
			end)
		end)
		if ds6:GetAsync(player.UserId, petone.Value) == petone.Value then
			print("saved")
			end

	end)
	
	pettwo.Changed:Connect(function()
		local w, l = pcall(function()
			ds7:UpdateAsync(playerkey, function(oldvalue)
				return player.leaderstats.pets.two.Value
			end)
		end)
		if ds7:GetAsync(player.UserId, pettwo.Value) == pettwo.Value then
			print("saved")
		end
	end)
	
	petthree.Changed:Connect(function()
		local w, l = pcall(function()
			ds8:UpdateAsync(playerkey, function(oldvalue)
				return player.leaderstats.pets.three.Value
			end)
		end)
		if ds8:GetAsync(player.UserId, petthree.Value) == petthree.Value then
			print("saved")
		end
	end)
	
	petfour.Changed:Connect(function()
		local w, l = pcall(function()
			ds9:UpdateAsync(playerkey, function(oldvalue)
				return player.leaderstats.pets.four.Value
			end)
		end)
		if ds9:GetAsync(player.UserId, petfour.Value) == petfour.Value then
			print("saved")
		end
	end)
	
	petfive.Changed:Connect(function()
		local w, l = pcall(function()
			ds10:UpdateAsync(playerkey, function(oldvalue)
				return player.leaderstats.pets.five.Value
			end)
		end)
		if ds10:GetAsync(player.UserId, petfive.Value) == petfive.Value then
			print("saved")
		end
	end)
	
	petsix.Changed:Connect(function()
		local w, l = pcall(function()
			ds11:UpdateAsync(playerkey, function(oldvalue)
				return player.leaderstats.pets.six.Value
			end)
		end)
		if ds11:GetAsync(player.UserId, petsix.Value) == petsix.Value then
			print("saved")
		end
	end)
	
	Yums.Changed:Connect(function()
		local w, l = pcall(function(plr)
			ds1:UpdateAsync(playerkey, function(oldvalue)
				return player.leaderstats.Yums.Value
				
			end)
		end)
		if ds1:GetAsync(player.UserId, Yums.Value) == Yums.Value then
			print("saved")
			end

end)
	burger.Changed:Connect(function()
		local w, l = pcall(function()
			ds2:UpdateAsync(playerkey, function(oldvalue)
				return player.leaderstats.burger.Value
			end)
		end)
		if ds2:GetAsync(player.UserId, Yums.Value) == Yums.Value then
			print("saved")
		end
	end)
	
	
	rebirth.Changed:connect(function()
		local w, l = pcall(function()
			ds3:UpdateAsync(playerkey, function(oldvalue)
				return player.leaderstats.Rebirth.Value
			end)
		end)
		if ds3:GetAsync(player.UserId, rebirth.Value) == rebirth.Value then
			print("saved")
			end
		end)
	game.Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function(char)
			char.Changed:Connect(function()
				if ds4:GetAsync(player.UserId, Yums.Value) == Yums.Value then
					print("saved")
				end
			end)
		end)
	end)

end)


game.Players.PlayerRemoving:connect(function(player)
	local w, l = pcall(function()
			ds1:UpdateAsync(player.UserId, function(oldvalue)
				return player.leaderstats.Yums.Value
			end)
	end)
	if w then
		print("load succes")
	end
	if l then
		warn(l)
	end	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local w2, l2 = pcall(function()
		ds2:UpdateAsync(player.UserId, function(oldvalue)
			return player.leaderstats.burger.Value
		end)
	end)
	if w2 then
		print("load succes")
	end
	if l2 then
		warn(l2)
	end	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local w3, l3 = pcall(function()
		ds3:UpdateAsync(player.UserId, function(oldvalue)
			return player.leaderstats.Rebirth.Value
		end)
	end)
	if w3 then
		print("load succes")
	end
	if l3 then
		warn(l3)
	end	
end)


game.Players.PlayerRemoving:Connect(function(player)
	local w4, l4 = pcall(function()
		ds6:UpdateAsync(player.UserId, function(oldvalue)
			return player.leaderstats.pets.one.Value
		end)
	end)
	if w4 then
		print("load succes")
	end
	if l4 then
		warn(l4)
	end	
end)


game.Players.PlayerRemoving:Connect(function(player)
	local w5, l5 = pcall(function()
		ds7:UpdateAsync(player.UserId, function(oldvalue)
			return player.leaderstats.pets.two.Value
		end)
	end)
	if w5 then
		print("load succes")
	end
	if l5 then
		warn(l5)
	end	
end)


game.Players.PlayerRemoving:Connect(function(player)
	local w6, l6 = pcall(function()
		ds8:UpdateAsync(player.UserId, function(oldvalue)
			return player.leaderstats.pets.three.Value
		end)
	end)
	if w6 then
		print("load succes")
	end
	if l6 then
		warn(l6)
	end	
end)


game.Players.PlayerRemoving:Connect(function(player)
	local w7, l7 = pcall(function()
		ds9:UpdateAsync(player.UserId, function(oldvalue)
			return player.leaderstats.pets.four.Value
		end)
	end)
	if w7 then
		print("load succes")
	end
	if l7 then
		warn(l7)
	end	
end)


game.Players.PlayerRemoving:Connect(function(player)
	local w8, l8 = pcall(function()
		ds10:UpdateAsync(player.UserId, function(oldvalue)
			return player.leaderstats.pets.five.Value
		end)
	end)
	if w8 then
		print("load succes")
	end
	if l8 then
		warn(l8)
	end	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local w9, l9 = pcall(function()
		ds11:UpdateAsync(player.UserId, function(oldvalue)
			return player.leaderstats.pets.six.Value
		end)
	end)
	print("saved")
	if w9 then
		print("load succes")
	end
	if l9 then
		warn(l9)
	end	
end)

I already added updateasync by value changes instead of setasync and used pcall functions to dectect errors

Maybe don’t use 11 datastores? Just save the pets in a table

3 Likes

yes thats a great idea. thanks

I think I would save all the pet values in one datastore instead of six

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("YumsSave")
local ds2 = DataStore:GetDataStore("BurgerSave")
local ds3 = DataStore:GetDataStore("Rebirthsave")
local ds4 = DataStore:GetDataStore("Petvaluesave")
local ds5 = DataStore:GetDataStore("PetSave")
local ds6 = DataStore:GetDataStore("PetSavevalue")
local try = 0
local waitt = 0

local function save(player)
	local playerkey = player.UserId
	
	local tableToSave = {
		player.leaderstats.pets.one.Value;
		player.leaderstats.pets.two.Value;
		player.leaderstats.pets.three.Value;
		player.leaderstats.pets.four.Value;
		player.leaderstats.pets.five.Value;
		player.leaderstats.pets.six.Value;
	}
	local success, err = pcall(function()
		ds6:SetAsync(playerkey, tableToSave)
	end)
	
	if success then
		print("saved")
	else
		print(err)
	end
end

game.Players.PlayerAdded:connect(function(player)
	local dataplayer = ds6:GetAsync(player.UserId)
	local playerkey = player.UserId
	print("stats")
	local leader = Instance.new("Folder")
	leader.Name = "leaderstats"
	leader.Parent = player  

	local pet = Instance.new("NumberValue")
	pet.Name = "petvalue"
	pet.Parent = player
	pet.Value = 0
	print("pet value loaded")

	local Yums = Instance.new("IntValue") 
	Yums.Name = "Yums"
	Yums.Parent = leader
	local w4, l4 = pcall(function()
		Yums.Value = ds1:GetAsync(player.UserId)
		ds1:SetAsync(player.UserId, Yums.Value)
	end)
	print("Yums loaded")
	print(script.Parent)

	local burger = Instance.new("IntValue") 
	burger.Name = "burger"
	burger.Parent = leader
	local w3, l3 = pcall(function()
		burger.Value = ds2:GetAsync(player.UserId)
		ds2:SetAsync(player.UserId, burger.Value)
	end)
	print("burger level loaded")

	local rebirth = Instance.new("IntValue")
	rebirth.Name = "Rebirth"
	rebirth.Parent = leader
	local w5, l5 = pcall(function()
		rebirth.Value = ds3:GetAsync(player.UserId)
		ds3:SetAsync(player.UserId, rebirth.Value)
	end)
	print("rebirths")

	local pets = Instance.new("Folder")
	pets.Name = "pets"
	pets.Parent = leader

	local petone = Instance.new("IntValue")
	petone.Parent = pets
	petone.Name = "one"
	print("pet1 loaded")

	local pettwo = Instance.new("IntValue")
	pettwo.Parent = pets
	pettwo.Name = "two"
	print("pet2 loaded")

	local petthree = Instance.new("IntValue")
	petthree.Parent = pets
	petthree.Name = "three"
	print("pet3 loaded")

	local petfour = Instance.new("IntValue")
	petfour.Parent = pets
	petfour.Name = "four"
	print("pet4 loaded")

	local petfive = Instance.new("IntValue")
	petfive.Parent = pets
	petfive.Name = "five"
	print("pet5 loaded")

	local petsix = Instance.new("IntValue")
	petsix.Parent = pets
	petsix.Name = "six"
	print("pet6 loaded")

	if w3 then
		print("load succes")
	end
	if l3 then
		warn(l3)
	end	
	if w4 then
		print("load succes")
	end
	if l4 then
		warn(l4)
	end	
	if w5 then
		print("load succes")
	end
	if l5 then
		warn(l5)
	end	

	local data
	local suc, err = pcall(function()
		data = ds6:GetAsync(player.UserId)
	end)	
	
	if suc and data then
		petone.Value = data[1]
		pettwo.Value = data[2]
		petthree.Value = data[3]
		petfour.Value = data[4]
		petfive.Value = data[5]
		petsix.Value = data[6]
	else
		print("the player has no data")
	end
	
	
	petone.Changed:Connect(function()
		local success, err  = pcall(function()
			save(player) 
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!"..err)
		end

	end)
	
	pettwo.Changed:Connect(function()
		local success, err  = pcall(function()
			save(player) 
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!"..err)
		end
	end)
	
	petthree.Changed:Connect(function()
		local success, err  = pcall(function()
			save(player) 
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!"..err)
		end
	end)
	
	petfour.Changed:Connect(function()
		local success, err  = pcall(function()
			save(player) 
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!"..err)
		end
	end)
	
	petfive.Changed:Connect(function()
		local success, err  = pcall(function()
			save(player) 
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!"..err)
		end
	end)
	
	petsix.Changed:Connect(function()
		local success, err  = pcall(function()
			save(player) 
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!"..err)
		end
	end)
	
	Yums.Changed:Connect(function()
		local w, l = pcall(function(plr)
			ds1:UpdateAsync(playerkey, function(oldvalue)
				return player.leaderstats.Yums.Value
				
			end)
		end)
		if ds1:GetAsync(player.UserId, Yums.Value) == Yums.Value then
			print("saved")
			end

end)
	burger.Changed:Connect(function()
		local w, l = pcall(function()
			ds2:UpdateAsync(playerkey, function(oldvalue)
				return player.leaderstats.burger.Value
			end)
		end)
		if ds2:GetAsync(player.UserId, Yums.Value) == Yums.Value then
			print("saved")
		end
	end)
	
	
	rebirth.Changed:connect(function()
		local w, l = pcall(function()
			ds3:UpdateAsync(playerkey, function(oldvalue)
				return player.leaderstats.Rebirth.Value
			end)
		end)
		if ds3:GetAsync(player.UserId, rebirth.Value) == rebirth.Value then
			print("saved")
			end
		end)
	game.Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function(char)
			char.Changed:Connect(function()
				if ds4:GetAsync(player.UserId, Yums.Value) == Yums.Value then
					print("saved")
				end
			end)
		end)
	end)

end)


game.Players.PlayerRemoving:connect(function(player)
	local w, l = pcall(function()
			ds1:UpdateAsync(player.UserId, function(oldvalue)
				return player.leaderstats.Yums.Value
			end)
	end)
	if w then
		print("load succes")
	end
	if l then
		warn(l)
	end	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local w2, l2 = pcall(function()
		ds2:UpdateAsync(player.UserId, function(oldvalue)
			return player.leaderstats.burger.Value
		end)
	end)
	if w2 then
		print("load succes")
	end
	if l2 then
		warn(l2)
	end	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local w3, l3 = pcall(function()
		ds3:UpdateAsync(player.UserId, function(oldvalue)
			return player.leaderstats.Rebirth.Value
		end)
	end)
	if w3 then
		print("load succes")
	end
	if l3 then
		warn(l3)
	end	
end)

game.Players.PlayerRemoving:Connect(function(player) 
		local success, err  = pcall(function()
			save(player) 
		end)

		if success then
			print("Data has been saved")
		else
		print("Data has not been saved!"..err)
	end
end)


game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do 
		local success, err  = pcall(function()
			save(player) 
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!"..err)
		end
	end
end)

is the leaderstats better now? I’m saving all my petvalues in ds6 now

Maybe use dictionaries to store the other values to.
If you do this correctly you only have to use 1 datastore.
Documentation: Tables

Okay, I think I gonna do that.