DataStore Is not Saving Value

Hello. This time, the datastore is not saving value.

local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")




Players.PlayerAdded:Connect(function(player)
	
	local playerId = player.UserId
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local PlayerValue = Instance.new("Folder")
	PlayerValue.Name = "PlayerValue"
	PlayerValue.Parent = player
	
	
	

	
	local Wins = Instance.new("IntValue")
	Wins.Name = "a"
	Wins.Parent = leaderstats
	
	local streak = Instance.new("IntValue")
	streak.Name = "b"
	streak.Parent = leaderstats
	
	local RP = Instance.new("IntValue")
	RP.Name = "c"
	RP.Parent = leaderstats
	
	local death = Instance.new("IntValue")
	death.Name = "d"
	death.Parent = leaderstats

	
	local onix = Instance.new("IntValue")
	onix.Name = "e"
	onix.Parent = PlayerValue
	
	
	Wins.Value = DataStore:GetAsync(playerId.."-a")
	streak.Value = DataStore:GetAsync(playerId.."-b")
	death.Value = DataStore:GetAsync(playerId.."-d")
	RP.Value = DataStore:GetAsync(playerId.."-c")
	onix.Value = DataStore:GetAsync(playerId.."-e")

	
end)


Players.PlayerRemoving:Connect(function(player)
	
	local leaderstats = player:FindFirstChild("leaderstats")
	local PlayerValue = player:FindFirstChild("PlayerValue")

	DataStore:SetAsync(player.UserId.."-a",leaderstats.a.Value)
	DataStore:SetAsync(player.UserId.."-d",leaderstats.d.Value)
	DataStore:SetAsync(player.UserId.."-c",leaderstats.c.Value)
	DataStore:SetAsync(player.UserId.."-b",leaderstats.b.Value)
	DataStore:SetAsync(player.UserId.."-e",PlayerValue.e.Value)



	print("DataSaved")
end)


and then, “DataSaved” is not printing and data is not saving

If youre going to save multiple values, use a table, not different keys.

I can assume this is because the DataStores are getting overloaded, and stop saving data.

local DSS = game:GetService('DataStoreService')
local DataStore = DSS:GetDataStore('Test')

function LoadData(Player)
	local leaderstats = Instance.new('Folder', Player)
	leaderstats.Name = 'leaderstats'
	
	local A_Value = Instance.new('IntValue', leaderstats)
	A_Value.Name = 'A'

	local B_Value = Instance.new('IntValue', leaderstats)
	B_Value.Name = 'B'
	
	local LoadedData = {}
	local success, errorMessage = pcall(function()
		LoadedData = DataStore:GetAsync(Player.UserId)
	end)
	
	if success and LoadedData ~= nil then
		A_Value.Value = LoadedData.A
		B_Value.Value = LoadedData.B
	end
end

function SaveData(Player)
	local DataToSave = {
		["A"] = Player.leaderstats.A.Value;
		["B"] = Player.leaderstats.B.Value;
	}
	DataStore:SetAsync(Player.UserId, DataToSave)
end

game.Players.PlayerAdded:Connect(LoadData)
game.Players.PlayerRemoving:Connect(SaveData)
1 Like

You could be calling SetAsync/GetAsync too many times, and you are not checking the return values from the call to the DataStore (common practice is to wrap them in a pcall()).

You could stick all of these values into a table and use Get/SetAsync just once, on the table i.e.

local dataTemplate = {Wins=0,Streak=0,RP=0,Death=0,Onix=0}

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