I dont why this script does not save data. If I take out if not game:GetService("RunService"):IsStudio() then, then it'll save data, but it gives me an error dataStore request was added to queue.

local DataStore = game:GetService("DataStoreService"):GetDataStore("BM_Statpoints_LevelSystem_DataStore_1") -- we create a new data store

local function Save(Player) 
	local saveTable = {}
	for i = 1, #Player:WaitForChild("Data"):GetChildren() do 
		if i == 1 then 
			saveTable[1] = Player:WaitForChild("Data"):WaitForChild("Level").Value 
		elseif i == 2 then 
			saveTable[2] = Player:WaitForChild("Data"):WaitForChild("Exp").Value 
		elseif i == 3 then 
			saveTable[3] = Player:WaitForChild("Data"):WaitForChild("ExpMax").Value 
		elseif i == 4 then 
			saveTable[4] = Player:WaitForChild("Data"):WaitForChild("Statpoints").Value 
		elseif i == 5 then 
			saveTable[5] = Player:WaitForChild("Data"):WaitForChild("Strength").Value
		elseif i == 6 then 
			saveTable[6] = Player:WaitForChild("Data"):WaitForChild("Speed").Value 
		elseif i == 7 then 
			saveTable[7] = Player:WaitForChild("Data"):WaitForChild("Intelligence").Value 
		elseif i == 8 then 
			saveTable[8] = Player:WaitForChild("Data"):WaitForChild("Defense").Value 
		end
	end
	DataStore:SetAsync(Player.UserId, saveTable) 
end

game.Players.PlayerAdded:Connect(function(Player) 
	
	local folder = Instance.new("Folder", Player) 
	folder.Name = "Data"
	
	local level = Instance.new("IntValue", folder) 
	level.Name = "Level"
	
	local exp = Instance.new("IntValue", folder) 
	exp.Name = "Exp"
	
	local expmax = Instance.new("IntValue", folder) 
	expmax.Name = "ExpMax"
	
	local statpoints = Instance.new("IntValue", folder)  
	statpoints.Name = "Statpoints"
	
	local strength = Instance.new("IntValue", folder) 
	strength.Name = "Strength"
	
	local speed = Instance.new("IntValue", folder)  
	speed.Name = "Speed"
	
	local intelligence = Instance.new("IntValue", folder)  
	intelligence.Name = "Intelligence"
	
	local defense = Instance.new("IntValue", folder)  
	defense.Name = "Defense"
	
	local success, errormessage = pcall(function() 
		local retrievedData = DataStore:GetAsync(Player.UserId)
		if retrievedData then 
			for i, value in pairs(retrievedData)do 
				if i == 1 then
					level.Value = value  
				elseif i == 2 then 
					exp.Value = value 
				elseif i == 3 then 
					expmax.Value = value  
				elseif i == 4 then 
					statpoints.Value = value  
				elseif i == 5 then 
					strength.Value = value  
				elseif i == 6 then 
					speed.Value = value  
				elseif i == 7 then 
					intelligence.Value = value  
				elseif i == 8 then 
					defense.Value = value  
				end
			end
		else 
			level.Value = 1
			exp.Value = 0
			expmax.Value = 100
			statpoints.Value = 0
			strength.Value = 0
			speed.Value = 0
			intelligence.Value = 0
			defense.Value = 0
		end
	end)
	if success then 
		print("Successfully loaded " ..Player.Name.."'s data.")
	end
	if errormessage then 
		error("Error while loading ".. Player.Name.."'s data: "..errormessage)
	end
	
end)

if not game:GetService("RunService"):IsStudio() then
	game:BindToClose(function() 
		for i, Player in pairs(game.Players:GetChildren())do 
			Save(Player) 
		end
	end)
end


game.Players.PlayerRemoving:Connect(function(Player) 
	Save(Player) 
end)

2 comments

0

You completely nullified the purpose of your first for loop by having so many if ... elseif statements. Use the index of the loop for each number. Then, you literally just need a single if statement.

0

Can you give me an example. I am new.