Right now I'm trying to make a game based off of combat. Because of this, I have multiple values that I want to save. I just recently tried storing them in a table, but this has caused more issues than it has solved.

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

local DS = game:GetService("DataStoreService")
local Data = game:GetService('DataStoreService'):GetDataStore('RoData3')

local prefix = 'user_'


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

game.Players.PlayerAdded:Connect(function(Player)
	
	--//Creating the stat values\\--
	
	local plrData = Data:GetAsync(prefix  .. tostring(Player.UserId))
	
	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
	local STVal = Instance.new("NumberValue")
	STVal.Parent = game.ServerStorage
	STVal.Name = "StatTimer"..Player.Name
	
	
	
	--//Loading the stat values into the created ones\\--
	
	local success, errormessage = pcall(function()
		
	if plrData then
          QVal.Value = plrData[1] or 0
         CVal.Value = plrData[2] or 1
TVal.Value = plrData[3] or 1
SVal.Value = plrData[4] or 1
IVal.Value = plrData[5] or 1
TRVal.Value = plrData[6] or 1
AVal.Value = plrData[7] or 6
ATVal.Value = plrData[8] or 30
STVal.Value = plrData[9] or 30

local numbers = {0.9,1,1.1}
	HVal.Value = plrData[10] or numbers[math.random(1,#numbers)]
local maxnumbers = {1,1.1,1.2,1.3,1.4}
	MVal.Value = plrData[11] or maxnumbers[math.random(1,#numbers)]



   else
        -- most people like to save the data if they have none 

    Data:SetAsync(prefix  .. tostring(Player.UserId), {
       QVal.Value,
        CVal.Value,    
TVal.Value, 
SVal.Value,   
IVal.Value,   
TRVal.Value,     
AVal.Value,    
ATVal.Value,  
STVal.Value,
HVal.Value,  
MVal.Value    
     })
   end
	
	--//Setting height value\\--
	
	

	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)
	local STVal = game.ServerStorage:FindFirstChild("StatTimer"..Player.Name)
	
	--//Loading the stat values into the created ones\\--
	
	local success, errormessage = pcall(function()
		 Data:SetAsync(prefix  .. tostring(Player.UserId), {
       QVal.Value,
        CVal.Value,    
TVal.Value, 
SVal.Value,   
IVal.Value,   
TRVal.Value,     
HVal.Value,    
MVal.Value,  
AVal.Value,
ATVal.Value,  
STVal.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)



I have seen many problems so far. For one, multiple values are getting saved and loaded into others, mixing them up. Another problem is that values like "Age" or "AVal" are having their value set to 1, not even 6, or what they are supposed to be saved as. The other values, such as "MaxHeight" or "MVal", and "Height" or "HVal", don't seem to be randomizing upon first playing the game. How do I fix these problems?

2 comments

0

Print out your data store and I’m sure it will help you. Just print(plrData) after you GetAsync the datastore

0

Right now I don’t know the structure of your datastore. I can guess but I’m not fully knowledgable. I’ve used datastore with tables inside tables inside tables perfectly fine. Just terrible at finding problems without doing it myself lol.