Hi. Recently I have been having an issue while testing my game that has been plaguing development. I made a save system to save stats for my games players, but a problem I seem to be having is that sometimes upon leaving and rejoining, players stat values will simply be '1'. I have no idea how this is happening or how I can fix this. This is the code:

--//Fetching the datastore service in order to load stat values\\--

local DS = game:GetService("DataStoreService")
local QuirkStore = DS:GetDataStore("RNHLstatsN")
local CombatStore = DS:GetDataStore("RNHLstatsN")
local TechniqueStore = DS:GetDataStore("RNHLstatsN")
local StaminaStore = DS:GetDataStore("RNHLstatsN")
local IntelligenceStore = DS:GetDataStore("RNHLstatsN")
local TryStore = DS:GetDataStore("RNHLstatsN")
local HeightStore = DS:GetDataStore("RNHLstatsN")
local AgeStore = DS:GetDataStore("RNHLstatsN")
local MHStore = DS:GetDataStore("RNHLstatsN")
local AgeTimer = DS:GetDataStore("RNHLstatsN")
local StatTimer = DS:GetDataStore("RNHLstatsN")

--//Firing when a new player joins\\--

game.Players.PlayerAdded:Connect(function(Player)
	
	--//Creating the stat values\\--
	
	local QVal = Instance.new("NumberValue")
	QVal.Parent = game.ServerStorage
	QVal.Name = "Quirk"..Player.Name
	local CVal = Instance.new("NumberValue")
	CVal.Parent = game.ServerStorage
	CVal.Name = "Combat"..Player.Name
	local TVal = Instance.new("NumberValue")
	TVal.Parent = game.ServerStorage
	TVal.Name = "Technique"..Player.Name
	local SVal = Instance.new("NumberValue")
	SVal.Parent = game.ServerStorage
	SVal.Name = "Stamina"..Player.Name
	local IVal = Instance.new("NumberValue")
	IVal.Parent = game.ServerStorage
	IVal.Name = "Intelligence"..Player.Name
	local TRVal = Instance.new("NumberValue")
	TRVal.Parent = game.ServerStorage
	TRVal.Name = "Tries"..Player.Name
	local HVal = Instance.new("NumberValue")
	HVal.Parent = game.ServerStorage
	HVal.Name = "Height"..Player.Name
	local MVal = Instance.new("NumberValue")
	MVal.Parent = game.ServerStorage
	MVal.Name = "MaxHeight"..Player.Name
	local AVal = Instance.new("NumberValue")
	AVal.Parent = game.ServerStorage
	AVal.Name = "Age"..Player.Name
	local ATVal = Instance.new("NumberValue")
	ATVal.Parent = game.ServerStorage
	ATVal.Name = "AgeTimer"..Player.Name
	
	
	
	--//Loading the stat values into the created ones\\--
	
	local success, errormessage = pcall(function()
	repeat wait() 
		QVal.Value = QuirkStore:GetAsync(Player.UserId.."Quirk") or 0
	until QVal.Value == QuirkStore:GetAsync(Player.UserId.."Quirk") or 0
	repeat wait() 
		CVal.Value = CombatStore:GetAsync(Player.UserId.."Combat") or 1
	until CVal.Value == CombatStore:GetAsync(Player.UserId.."Combat") or 1
	repeat wait() 
		TVal.Value = TechniqueStore:GetAsync(Player.UserId.."Technique") or 1
	until TVal.Value == TechniqueStore:GetAsync(Player.UserId.."Technique") or 1
	repeat wait() 
		SVal.Value = StaminaStore:GetAsync(Player.UserId.."Stamina") or 1
	until SVal.Value == StaminaStore:GetAsync(Player.UserId.."Stamina") or 1
	repeat wait() 
		IVal.Value = IntelligenceStore:GetAsync(Player.UserId.."Intel") or 1
	until IVal.Value == IntelligenceStore:GetAsync(Player.UserId.."Intel") or 1
	repeat wait() 
		TRVal.Value = TryStore:GetAsync(Player.UserId.."Tries") or 1
	until TRVal.Value == TryStore:GetAsync(Player.UserId.."Tries") or 1
	repeat wait() 
		AVal.Value = AgeStore:GetAsync(Player.UserId.."Age") or 6
	until AVal.Value == AgeStore:GetAsync(Player.UserId.."Age") or 6
	repeat wait() 
		ATVal.Value = AgeTimer:GetAsync(Player.UserId.."AgeTimer") or 30
	until ATVal.Value == AgeTimer:GetAsync(Player.UserId.."AgeTimer") or 30
	
	--//Setting height value\\--
	
	
	local numbers = {0.9,1,1.1}
	HVal.Value = HeightStore:GetAsync(Player.UserId.."Height") or numbers[math.random(1,#numbers)]
	
	--//Setting max height value\\--
	wait(0.1)
	
	local maxnumbers = {1,1.1,1.2,1.3,1.4}
	MVal.Value = MHStore:GetAsync(Player.UserId.."MaxHeight") or maxnumbers[math.random(1,#numbers)]
	
	if game.ServerStorage:FindFirstChild("Age"..Player.name).Value == 1 then
		game.ServerStorage:FindFirstChild("Age"..Player.name).Value = 1
		end
		if game.ServerStorage:FindFirstChild("Age"..Player.name).Value == 6 and game.ServerStorage:FindFirstChild("Tries"..Player.name).Value == 0 then
		game.ServerStorage:FindFirstChild("Tries"..Player.name).Value = 1
		end
	
	end)
	
	--//Reading if the stat loading was successful or not\\--
	
	if success then
			print("Loaded "..Player.Name.."'s stats successfully")
			workspace.Events.Loaded:FireClient(Player)
		else
			print("Error loading "..Player.Name.."'s stats")
			warn(errormessage)
	end

end)

--//Firing when a player leaves\\--

game.Players.PlayerRemoving:Connect(function(Player)
	
	--//Finding and making a variable out of the players stats\\--
	
	local QVal = game.ServerStorage:FindFirstChild("Quirk"..Player.Name)
	local CVal = game.ServerStorage:FindFirstChild("Combat"..Player.Name)
	local TVal = game.ServerStorage:FindFirstChild("Technique"..Player.Name)
	local SVal = game.ServerStorage:FindFirstChild("Stamina"..Player.Name)
	local IVal = game.ServerStorage:FindFirstChild("Intelligence"..Player.Name)
	local TRVal = game.ServerStorage:FindFirstChild("Tries"..Player.Name)
	local HVal = game.ServerStorage:FindFirstChild("Height"..Player.Name)
	local MVal = game.ServerStorage:FindFirstChild("MaxHeight"..Player.Name)
	local AVal = game.ServerStorage:FindFirstChild("Age"..Player.Name)
	local ATVal = game.ServerStorage:FindFirstChild("AgeTimer"..Player.Name)
	
	--//Loading the stat values into the created ones\\--
	
	local success, errormessage = pcall(function()
		QVal.Value = QuirkStore:SetAsync(Player.UserId.."Quirk",QVal.Value)
	wait(0.1)
	CVal.Value = CombatStore:SetAsync(Player.UserId.."Combat",CVal.Value)
	wait(0.1)
	TVal.Value = TechniqueStore:SetAsync(Player.UserId.."Technique",TVal.Value)
	wait(0.1)
	SVal.Value = StaminaStore:SetAsync(Player.UserId.."Stamina",SVal.Value)
	wait(0.1)
	IVal.Value = IntelligenceStore:SetAsync(Player.UserId.."Intel",IVal.Value)
	wait(0.1)
	TRVal.Value = TryStore:SetAsync(Player.UserId.."Tries",TRVal.Value)
	wait(0.1)
	HVal.Value = HeightStore:SetAsync(Player.UserId.."Height",HVal.Value)
	wait(0.1)
	MVal.Value = MHStore:SetAsync(Player.UserId.."MaxHeight",MVal.Value)
	wait(0.1)
	AVal.Value = AgeStore:SetAsync(Player.UserId.."Age",AVal.Value)
	wait(0.1)
	ATVal.Value = AgeTimer:SetAsync(Player.UserId.."AgeTimer",ATVal.Value)
	end)
	--//Reading if the stat loading was successful or not\\--
	
	if success then
			print("Saved "..Player.Name.."'s stats successfully")
		else
			print("Error saving "..Player.Name.."'s stats")
			warn(errormessage)
		end

end)



3 comments

0

I don't think you can save floats, so try replacing the NumberValues with IntValues

0

Also, assert everything in there into a single table, to use one datastore. To save it, use http service JSONEncode and JSONDecode functions.

0

Don't repeatedly made requests from DataStores. simply use a pcall()