Saving leaderstats error?

hey there!

I’m having this weird loading bug that randomly showed up. this is the third time in a row i have gotten this error.

heres the error:
image

and heres the code:


please help if you have the answer! thank you :slight_smile:

1 Like

Mind pasting the code in a code block?

1 Like

sure, it includes a lot of inventory saving tho

local success, errorMessage = pcall(function()
winsdata = datastore:GetAsync(player.UserId…“-wins”)
coinsdata = datastore:GetAsync(player.UserId…“-coins”)
shardsdata = datastore:GetAsync(player.UserId…“-shards”)
debugg = settingsDatastore:GetAsync(player.UserId…“-debug”)
mobile = settingsDatastore:GetAsync(player.UserId…“-mobile”)
TSens = settingsDatastore:GetAsync(player.UserId…“-TSens”)
fov = settingsDatastore:GetAsync(player.UserId…“-fov”)
wind = settingsDatastore:GetAsync(player.UserId…“-wind”)
walkingParticles = settingsDatastore:GetAsync(player.UserId…“-walkingParticles”)
TFOV = settingsDatastore:GetAsync(player.UserId…“-TFOV”)

	for i, owned in pairs(player.ShopItems.Trail:GetDescendants()) do -- trail
		if not player.Character:FindFirstChild("CanDetect") then
			local canDetect = Instance.new("BoolValue")
			canDetect.Parent = player.Character
			canDetect.Name = "CanDetect"
			canDetect.Value = true
		end
		if owned.Name == 'Owned' then
			if owned.Parent.Name == "Red" then
				redTrail_ = itemsDatastore:GetAsync(player.UserId.."-Red_trail", owned.Value)
			elseif owned.Parent.Name == "Blue" then
				blueTrail_ = itemsDatastore:GetAsync(player.UserId.."-Blue_trail", owned.Value)
			elseif owned.Parent.Name == "Yellow" then
				yellowTrail_ = itemsDatastore:GetAsync(player.UserId.."-Yellow_trail", owned.Value)
			elseif owned.Parent.Name == "Green" then
				greenTrail_ = itemsDatastore:GetAsync(player.UserId.."-Green_trail", owned.Value)
			elseif owned.Parent.Name == "White" then
				whiteTrail_ = itemsDatastore:GetAsync(player.UserId.."-White_trail", owned.Value)
			elseif owned.Parent.Name == "Black" then
				blackTrail_ = itemsDatastore:GetChildren(player.UserId.."-Black_trail", owned.Value)
			elseif owned.Parent.Name == "Orange" then
				orangeTrail_ = itemsDatastore:GetAsync(player.UserId.."-Orange_trail", owned.Value)
			elseif owned.Parent.Name == "Purple" then
				purpleTrail_ = itemsDatastore:GetAsync(player.UserId.."-Purple_trail", owned.Value)
			elseif owned.Parent.Name == "Rainbow" then
				rainbowTrail_ = itemsDatastore:GetAsync(player.UserId.."-Rainbow_trail", owned.Value)
			end
		end
	end
	for i, owned in pairs(player.ShopItems.BallSkins:GetDescendants()) do -- ballskins
		if not player.Character:FindFirstChild("CanDetect") then
			local canDetect = Instance.new("BoolValue")
			canDetect.Parent = player.Character
			canDetect.Name = "CanDetect"
			canDetect.Value = true
		end
		if owned.Name == 'Owned' then
			if owned.Parent.Name == "Oof" then
				oofSkin_ = itemsDatastore:GetAsync(player.UserId.."-Oof_ballskin", owned.Value)
			elseif owned.Parent.Name == "Blue" then
				blueSkin_ = itemsDatastore:GetAsync(player.UserId.."-Blue_ballskin", owned.Value)
			elseif owned.Parent.Name == "Yellow" then
				yellowSkin_ = itemsDatastore:GetAsync(player.UserId.."-Yellow_ballskin", owned.Value)
			elseif owned.Parent.Name == "Green" then
				greenSkin_ = itemsDatastore:GetAsync(player.UserId.."-Green_ballskin", owned.Value)
			elseif owned.Parent.Name == "White" then
				whiteSkin_ = itemsDatastore:GetAsync(player.UserId.."-White_ballskin", owned.Value)
			elseif owned.Parent.Name == "Black" then
				blackSkin_ = itemsDatastore:GetChildren(player.UserId.."-Black_ballskin", owned.Value)
			elseif owned.Parent.Name == "Orange" then
				orangeSkin_ = itemsDatastore:GetAsync(player.UserId.."-Orange_ballskin", owned.Value)
			elseif owned.Parent.Name == "Purple" then
				purpleSkin_ = itemsDatastore:GetAsync(player.UserId.."-Purple_ballskin", owned.Value)
			end
		end
	end
	
end)
if success then
	wins.Value = winsdata
	coins.Value = coinsdata
	shards.Value = shardsdata
	player.Settings.DebugMode.Value = debugg or false
	player.Settings.MobileControls.Value = mobile or false
	player.Settings.ThrowingSensitivity.Value = TSens or 0.2
	player.Settings.FOV.Value = fov or 70
	player.Settings.Wind.Value = wind or true
	player.Settings.WalkingParticles.Value = walkingParticles or true
	player.Settings.ThrowingFOV.Value = TFOV or 20
	for i, owned in pairs(player.ShopItems.Trail:GetDescendants()) do -- trail
		if owned.Name == 'Owned' then
			if owned.Parent.Name == "Red" then
				owned.Value = redTrail_
			elseif owned.Parent.Name == "Blue" then
				owned.Value = blueTrail_
			elseif owned.Parent.Name == "Yellow" then
				owned.Value = yellowTrail_
			elseif owned.Parent.Name == "Green" then
				owned.Value = greenTrail_
			elseif owned.Parent.Name == "White" then
				owned.Value = whiteTrail_
			elseif owned.Parent.Name == "Black" then
				owned.Value = blackTrail_
			elseif owned.Parent.Name == "Orange" then
				owned.Value = orangeTrail_
			elseif owned.Parent.Name == "Purple" then
				owned.Value = purpleTrail_
			elseif owned.Parent.Name == "Rainbow" then
				owned.Value = rainbowTrail_
			end
		end
	end
	for i, owned in pairs(player.ShopItems.BallSkins:GetDescendants()) do -- trail
		if owned.Name == 'Owned' then
			if owned.Parent.Name == "Oof" then
				owned.Value = oofSkin_
			elseif owned.Parent.Name == "Blue" then
				owned.Value = blueSkin_
			elseif owned.Parent.Name == "Yellow" then
				owned.Value = yellowSkin_
			elseif owned.Parent.Name == "Green" then
				owned.Value = greenSkin_
			elseif owned.Parent.Name == "White" then
				owned.Value = whiteSkin_
			elseif owned.Parent.Name == "Black" then
				owned.Value = blackSkin_
			elseif owned.Parent.Name == "Orange" then
				owned.Value = orangeSkin_
			elseif owned.Parent.Name == "Purple" then
				owned.Value = purpleSkin_
			end
		end
	end
	for i,object in ipairs(player.Character:GetDescendants()) do
		if object:IsA("BasePart") then
			PhysicsService:SetPartCollisionGroup(object, "Player")
		end	
	end
	
	print("Data loaded")
else
	print("Error when loading data: ")
	warn(errorMessage)
end

thats the loading code, but if you want the entire leaderstats code here it is:

local THROWING_MODULE = require(game.ServerScriptService.ServerModules.CATCHING_MODULE.THROWING_MODULE)

local PhysicsService = game:GetService("PhysicsService")
local DataStoreService = game:GetService("DataStoreService")
local MessagingService = game:GetService("MessagingService")

local datastore = DataStoreService:GetDataStore("DataStore")
local settingsDatastore = DataStoreService:GetDataStore("SettingsDatastore")
local itemsDatastore = DataStoreService:GetDataStore("ItemDatastore")

local save = game.ReplicatedStorage.Events.SaveSettings

local shopEvent = game.ReplicatedStorage.Events.ShopEvent

local function saveFunc(player)
	local character = player.Character
	local success, errorMessage = pcall(function()
		datastore:SetAsync(player.UserId.."-wins", player.leaderstats.Wins.Value)
		datastore:SetAsync(player.UserId.."-coins", player.leaderstats.Coins.Value)
		datastore:SetAsync(player.UserId.."-shards", player.leaderstats.Shards.Value)
		settingsDatastore:SetAsync(player.UserId.."-debug", player.Settings.DebugMode.Value)
		settingsDatastore:SetAsync(player.UserId.."-mobile", player.Settings.MobileControls.Value)
		settingsDatastore:SetAsync(player.UserId.."-TSens", player.Settings.ThrowingSensitivity.Value)
		settingsDatastore:SetAsync(player.UserId.."-fov", player.Settings.FOV.Value)
		settingsDatastore:SetAsync(player.UserId.."-wind", player.Settings.Wind.Value)
		settingsDatastore:SetAsync(player.UserId.."-walkingParticles", player.Settings.WalkingParticles.Value)
		settingsDatastore:SetAsync(player.UserId.."-TFOV", player.Settings.ThrowingFOV.Value)
		--SHOP ITEMS--

		--TRAILS
		for i, owned in pairs(player.ShopItems.Trail:GetDescendants()) do -- trail
			if owned.Name == 'Owned' then
				itemsDatastore:SetAsync(player.UserId.."-"..owned.Parent.Name.."_trail", owned.Value)
			end
		end
		
		--BALLSKINS
		for i, owned in pairs(player.ShopItems.BallSkins:GetDescendants()) do -- skins
			if owned.Name == 'Owned' then
				itemsDatastore:SetAsync(player.UserId.."-"..owned.Parent.Name.."_ballskin", owned.Value)
			end
		end
	end)
	if success == true then
		print("Data saved")
	else
		print("Error when saving data: ")
		warn(errorMessage)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local statss = Instance.new("Folder")
	statss.Name = "Stats"
	statss.Parent = player
	
	local throwing = Instance.new("BoolValue")
	throwing.Parent = statss
	throwing.Value = false
	throwing.Name = "Throwing"
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local wins = Instance.new("IntValue", leaderstats)
	wins.Value = 0
	wins.Name = "Wins"
	
	local coins = Instance.new("IntValue", leaderstats)
	coins.Value = 0
	coins.Name = "Coins"
	
	local shards = Instance.new("IntValue", leaderstats)
	shards.Value = 0
	shards.Name = "Shards"
	
	local setting = game.ServerStorage.Settings:Clone()
	setting.Parent = player
	
	local voted = Instance.new("BoolValue")
	voted.Parent = statss
	voted.Value = false
	voted.Name = "Voted"
	
	local team = Instance.new("StringValue")
	team.Parent = statss
	team.Name = "Team"
	team.Value = "None"
	
	local kills = Instance.new("IntValue")
	kills.Parent = statss
	kills.Name = "Kills"
	kills.Value = 0
	
	local currentQuest = Instance.new("StringValue")
	currentQuest.Parent = statss
	currentQuest.Name = "CurrentQuest"
	currentQuest.Value = "None"
	
	--shop items
	
	local shopItems = Instance.new("Folder")
	shopItems.Name = "ShopItems"
	shopItems.Parent = player
	
	local trails = Instance.new("Folder")
	trails.Name = "Trail"
	trails.Parent = shopItems
	
	local ballSkins = Instance.new("Folder")
	ballSkins.Name = "BallSkins"
	ballSkins.Parent = shopItems
	
	--TRAILS--
	
	local purpleTrail = Instance.new("BoolValue")
	purpleTrail.Name = "Purple"
	purpleTrail.Value = false
	purpleTrail.Parent = trails
	purpleTrail:SetAttribute("Price", 75)
	
	local orangeTrail = Instance.new("BoolValue")
	orangeTrail.Name = "Orange"
	orangeTrail.Value = false
	orangeTrail.Parent = trails
	orangeTrail:SetAttribute("Price", 75)
	
	local rainbowTrail = Instance.new("BoolValue")
	rainbowTrail.Name = "Rainbow"
	rainbowTrail.Value = false
	rainbowTrail.Parent = trails
	rainbowTrail:SetAttribute("Price", 125)
	
	local redTrail = Instance.new("BoolValue")
	redTrail.Name = "Red"
	redTrail.Value = false
	redTrail.Parent = trails
	redTrail:SetAttribute("Price", 50)
	
	local yellowTrail = Instance.new("BoolValue")
	yellowTrail.Name = "Yellow"
	yellowTrail.Value = false
	yellowTrail.Parent = trails
	yellowTrail:SetAttribute("Price", 50)
	
	local whiteTrail = Instance.new("BoolValue")
	whiteTrail.Name = "White"
	whiteTrail.Value = false
	whiteTrail.Parent = trails
	whiteTrail:SetAttribute("Price", 30)
	
	local blackTrail = Instance.new("BoolValue")
	blackTrail.Name = "Black"
	blackTrail.Value = false
	blackTrail.Parent = trails
	blackTrail:SetAttribute("Price", 30)
	
	local blueTrail = Instance.new("BoolValue")
	blueTrail.Name = "Blue"
	blueTrail.Value = false
	blueTrail.Parent = trails
	blueTrail:SetAttribute("Price", 50)
	
	local greenTrail = Instance.new("BoolValue")
	greenTrail.Name = "Green"
	greenTrail.Value = false
	greenTrail.Parent = trails
	greenTrail:SetAttribute("Price", 50)
	
	for i, trail in pairs(trails:GetChildren()) do
		local owned = Instance.new("BoolValue")
		owned.Value = false
		owned.Parent = trail
		owned.Name = "Owned"
		local character = player.Character or player.CharacterAdded:Wait()
		
		if not character:FindFirstChild("CanDetect") then
			local canDetect = Instance.new("BoolValue")
			canDetect.Parent = character
			canDetect.Name = "CanDetect"
			canDetect.Value = true
		end
	end
	
	--BALL SKINS--
	
	local purpleSkin = Instance.new("BoolValue")
	purpleSkin.Name = "Purple"
	purpleSkin.Value = false
	purpleSkin.Parent = ballSkins
	purpleSkin:SetAttribute("Price", 120)

	local orangeSkin = Instance.new("BoolValue")
	orangeSkin.Name = "Orange"
	orangeSkin.Value = false
	orangeSkin.Parent = ballSkins
	orangeSkin:SetAttribute("Price", 120)

	local yellowSkin = Instance.new("BoolValue")
	yellowSkin.Name = "Yellow"
	yellowSkin.Value = false
	yellowSkin.Parent = ballSkins
	yellowSkin:SetAttribute("Price", 100)

	local whiteSkin = Instance.new("BoolValue")
	whiteSkin.Name = "White"
	whiteSkin.Value = false
	whiteSkin.Parent = ballSkins
	whiteSkin:SetAttribute("Price", 60)
	
	local blackSkin = Instance.new("BoolValue")
	blackSkin.Name = "Black"
	blackSkin.Value = false
	blackSkin.Parent = ballSkins
	blackSkin:SetAttribute("Price", 60)

	local blueSkin = Instance.new("BoolValue")
	blueSkin.Name = "Blue"
	blueSkin.Value = false
	blueSkin.Parent = ballSkins
	blueSkin:SetAttribute("Price", 100)

	local greenSkin = Instance.new("BoolValue")
	greenSkin.Name = "Green"
	greenSkin.Value = false
	greenSkin.Parent = ballSkins
	greenSkin:SetAttribute("Price", 100)
	
	local oofSkin = Instance.new("BoolValue")
	oofSkin.Name = "Oof"
	oofSkin.Value = false
	oofSkin.Parent = ballSkins
	oofSkin:SetAttribute("Price", 225)
	
	for i, skin in pairs(ballSkins:GetChildren()) do
		local owned = Instance.new("BoolValue")
		owned.Value = false
		owned.Parent = skin
		owned.Name = "Owned"
		local character = player.Character or player.CharacterAdded:Wait()

		if not character:FindFirstChild("CanDetect") then
			local canDetect = Instance.new("BoolValue")
			canDetect.Parent = character
			canDetect.Name = "CanDetect"
			canDetect.Value = true
		end
	end
	
	local winsdata
	local coinsdata
	local shardsdata
	local debugg
	local mobile
	local TSens
	local fov
	local wind
	local walkingParticles
	local TFOV
	
	--trails
	local redTrail_ = nil
	local blueTrail_ = nil
	local yellowTrail_ = nil
	local greenTrail_ = nil
	local whiteTrail_ = nil
	local blackTrail_ = nil
	local orangeTrail_ = nil
	local purpleTrail_ = nil
	local rainbowTrail_ = nil
	
	--ballskins
	local blueSkin_ = nil
	local yellowSkin_ = nil
	local greenSkin_ = nil
	local whiteSkin_ = nil
	local blackSkin_ = nil
	local orangeSkin_ = nil
	local purpleSkin_ = nil
	local oofSkin_ = nil
	
	
	
	local success, errorMessage = pcall(function()
		winsdata = datastore:GetAsync(player.UserId.."-wins")
		coinsdata = datastore:GetAsync(player.UserId.."-coins")
		shardsdata = datastore:GetAsync(player.UserId.."-shards")
		debugg = settingsDatastore:GetAsync(player.UserId.."-debug") 
		mobile = settingsDatastore:GetAsync(player.UserId.."-mobile")
		TSens = settingsDatastore:GetAsync(player.UserId.."-TSens")
		fov = settingsDatastore:GetAsync(player.UserId.."-fov")
		wind = settingsDatastore:GetAsync(player.UserId.."-wind")
		walkingParticles = settingsDatastore:GetAsync(player.UserId.."-walkingParticles")
		TFOV = settingsDatastore:GetAsync(player.UserId.."-TFOV")
		
		for i, owned in pairs(player.ShopItems.Trail:GetDescendants()) do -- trail
			if not player.Character:FindFirstChild("CanDetect") then
				local canDetect = Instance.new("BoolValue")
				canDetect.Parent = player.Character
				canDetect.Name = "CanDetect"
				canDetect.Value = true
			end
			if owned.Name == 'Owned' then
				if owned.Parent.Name == "Red" then
					redTrail_ = itemsDatastore:GetAsync(player.UserId.."-Red_trail", owned.Value)
				elseif owned.Parent.Name == "Blue" then
					blueTrail_ = itemsDatastore:GetAsync(player.UserId.."-Blue_trail", owned.Value)
				elseif owned.Parent.Name == "Yellow" then
					yellowTrail_ = itemsDatastore:GetAsync(player.UserId.."-Yellow_trail", owned.Value)
				elseif owned.Parent.Name == "Green" then
					greenTrail_ = itemsDatastore:GetAsync(player.UserId.."-Green_trail", owned.Value)
				elseif owned.Parent.Name == "White" then
					whiteTrail_ = itemsDatastore:GetAsync(player.UserId.."-White_trail", owned.Value)
				elseif owned.Parent.Name == "Black" then
					blackTrail_ = itemsDatastore:GetChildren(player.UserId.."-Black_trail", owned.Value)
				elseif owned.Parent.Name == "Orange" then
					orangeTrail_ = itemsDatastore:GetAsync(player.UserId.."-Orange_trail", owned.Value)
				elseif owned.Parent.Name == "Purple" then
					purpleTrail_ = itemsDatastore:GetAsync(player.UserId.."-Purple_trail", owned.Value)
				elseif owned.Parent.Name == "Rainbow" then
					rainbowTrail_ = itemsDatastore:GetAsync(player.UserId.."-Rainbow_trail", owned.Value)
				end
			end
		end
		for i, owned in pairs(player.ShopItems.BallSkins:GetDescendants()) do -- ballskins
			if not player.Character:FindFirstChild("CanDetect") then
				local canDetect = Instance.new("BoolValue")
				canDetect.Parent = player.Character
				canDetect.Name = "CanDetect"
				canDetect.Value = true
			end
			if owned.Name == 'Owned' then
				if owned.Parent.Name == "Oof" then
					oofSkin_ = itemsDatastore:GetAsync(player.UserId.."-Oof_ballskin", owned.Value)
				elseif owned.Parent.Name == "Blue" then
					blueSkin_ = itemsDatastore:GetAsync(player.UserId.."-Blue_ballskin", owned.Value)
				elseif owned.Parent.Name == "Yellow" then
					yellowSkin_ = itemsDatastore:GetAsync(player.UserId.."-Yellow_ballskin", owned.Value)
				elseif owned.Parent.Name == "Green" then
					greenSkin_ = itemsDatastore:GetAsync(player.UserId.."-Green_ballskin", owned.Value)
				elseif owned.Parent.Name == "White" then
					whiteSkin_ = itemsDatastore:GetAsync(player.UserId.."-White_ballskin", owned.Value)
				elseif owned.Parent.Name == "Black" then
					blackSkin_ = itemsDatastore:GetChildren(player.UserId.."-Black_ballskin", owned.Value)
				elseif owned.Parent.Name == "Orange" then
					orangeSkin_ = itemsDatastore:GetAsync(player.UserId.."-Orange_ballskin", owned.Value)
				elseif owned.Parent.Name == "Purple" then
					purpleSkin_ = itemsDatastore:GetAsync(player.UserId.."-Purple_ballskin", owned.Value)
				end
			end
		end
		
	end)
	if success then
		wins.Value = winsdata
		coins.Value = coinsdata
		shards.Value = shardsdata
		player.Settings.DebugMode.Value = debugg or false
		player.Settings.MobileControls.Value = mobile or false
		player.Settings.ThrowingSensitivity.Value = TSens or 0.2
		player.Settings.FOV.Value = fov or 70
		player.Settings.Wind.Value = wind or true
		player.Settings.WalkingParticles.Value = walkingParticles or true
		player.Settings.ThrowingFOV.Value = TFOV or 20
		for i, owned in pairs(player.ShopItems.Trail:GetDescendants()) do -- trail
			if owned.Name == 'Owned' then
				if owned.Parent.Name == "Red" then
					owned.Value = redTrail_
				elseif owned.Parent.Name == "Blue" then
					owned.Value = blueTrail_
				elseif owned.Parent.Name == "Yellow" then
					owned.Value = yellowTrail_
				elseif owned.Parent.Name == "Green" then
					owned.Value = greenTrail_
				elseif owned.Parent.Name == "White" then
					owned.Value = whiteTrail_
				elseif owned.Parent.Name == "Black" then
					owned.Value = blackTrail_
				elseif owned.Parent.Name == "Orange" then
					owned.Value = orangeTrail_
				elseif owned.Parent.Name == "Purple" then
					owned.Value = purpleTrail_
				elseif owned.Parent.Name == "Rainbow" then
					owned.Value = rainbowTrail_
				end
			end
		end
		for i, owned in pairs(player.ShopItems.BallSkins:GetDescendants()) do -- trail
			if owned.Name == 'Owned' then
				if owned.Parent.Name == "Oof" then
					owned.Value = oofSkin_
				elseif owned.Parent.Name == "Blue" then
					owned.Value = blueSkin_
				elseif owned.Parent.Name == "Yellow" then
					owned.Value = yellowSkin_
				elseif owned.Parent.Name == "Green" then
					owned.Value = greenSkin_
				elseif owned.Parent.Name == "White" then
					owned.Value = whiteSkin_
				elseif owned.Parent.Name == "Black" then
					owned.Value = blackSkin_
				elseif owned.Parent.Name == "Orange" then
					owned.Value = orangeSkin_
				elseif owned.Parent.Name == "Purple" then
					owned.Value = purpleSkin_
				end
			end
		end
		for i,object in ipairs(player.Character:GetDescendants()) do
			if object:IsA("BasePart") then
				PhysicsService:SetPartCollisionGroup(object, "Player")
			end	
		end
		
		print("Data loaded")
	else
		print("Error when loading data: ")
		warn(errorMessage)
	end
	
	player.CharacterAdded:Connect(function(character)
		local trailAttach1 = Instance.new("Attachment")
		trailAttach1.Name = "TrailAttachment1"
		trailAttach1.Parent = character.HumanoidRootPart
		trailAttach1.WorldPosition = character.HumanoidRootPart.Position + Vector3.new(0,1.2,0)

		local trailAttach2 = Instance.new("Attachment")
		trailAttach2.Name = "TrailAttachment2"
		trailAttach2.Parent = character.HumanoidRootPart
		trailAttach2.WorldPosition = character.HumanoidRootPart.Position + Vector3.new(0,-1,0)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errorMessage = pcall(function()
		saveFunc(player)
	end)
	if success == true then
		print("saving...")
	else
		warn(errorMessage)
	end
	
end)

MessagingService:SubscribeAsync("Save", function(player)
	
	local success, errorMessage = pcall(function()
		saveFunc(game.Players:FindFirstChild(player.Data))
	end)
	if success == true then
		print("saving...")
	else
		warn(errorMessage)
	end
end)

game:BindToClose(function()
	for _, player in ipairs(game.Players:GetPlayers()) do
		saveFunc(player)
	end
end)

Alright Ill look into it rq for ya :slight_smile:

1 Like

holy damm youre 100% going to hit the limit

please try optimising this

this will not work realistically if 3 players join in a minute

I would recommend changing your code so that what they own and other stats are all in one table and you just load and save that table.

Having a datastore for each item is just about the worst way to do this. You’re going over the limit of datastore calls about halfway through loading one player’s data…

As @MrLonely1221 and Roblox states, use as few DataStores as possible.

Instead of having a singular DataStore for each item and stat, we can simply do

local playerStats = {
	Stats = {
		Wins = 0,
		Coins = 0,
		Shards = 0
	},
	Settings = {
		...
	},
	Inventory = {
		TrailA = true, -- owned
		TrailB = false -- not owned
	}
} 

We only need one datastore here! If you want, you can use three, however I’d just use one.

Trust me, your code will look way better, and looking for issues will be a piece of cake.

Using that many DataStores will eventually hit you with a rate limit. And now if you want to add more items it’ll be as simple as just adding a line or two.

Plus, try using string concatenation (or interpolation)! Instead of doing

elseif owned.Parent.Name == "White" then
	owned.Value = whiteTrail_

you can do…

owned.Value = Data.Inventory.Trails[`{string.lower(owned.Parent.Name)}Trail_`]

…from your datastore.

So easy! No need for elseifs!

1 Like

Don’t set string values with a instance, use instance.name or return a tostring

You only need one value (PlayerId) for GetAsync, you are getting the value from the datastore already. This will stop the error completely
Yea, as the people above said, you should optimize it into tables for more efficient saving

good idea! what would be the best way to implement this though, i’m still a little confused on how it would be different for each player.
thank you :slight_smile:

nevermind! i just figured it out. thank you so much for the advice this works so much better :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.