How do I get the values from the datastore table and add them to the IntValues inside the player

i want to get the values from the table thats saved and add them to the level/hp/stamina/mana values inside the player (and do i need to change anything else to the script to make it better)

local DataStoreService = game:GetService("DataStoreService")
local StatsData = DataStoreService:GetDataStore("leaderStatsData")
local Players = game:GetService("Players")

local AllValues = {
	LevelValue = nil,
	ManaValue = nil,
	StaminaValue = nil,
	HpValue = nil
}


game.Players.PlayerAdded:Connect(function(player)     
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 0
	Level.Parent = player
		
	local Mana = Instance.new("IntValue")
	Mana.Name = "Mana"
	Mana.Value = 0
	Mana.Parent = player
		
	local Stamina = Instance.new("IntValue")
	Stamina.Name = "Stamina"
	Stamina.Value = 0
	Stamina.Parent = player
		
	local Hp = Instance.new("IntValue")
	Hp.Name = "Hp"
	Hp.Value = 0
	Hp.Parent = player

	local playerUserId = "Player_" ..player.UserId

	local data
	local success, errorMessage = pcall(function()
		data = StatsData:GetAsync(playerUserId)
	end)
	
	if success  then
		--what do i put here to get the values from the table to the hp value stamina value mana value and lvl value
	else
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	AllValues = {
		LevelValue = player.Level.Value,
		ManaValue = player.Mana.Value,
		StaminaValue = player.Stamina.Value,
		HpValue = player.Hp.Value
	}
	
	local playerUserId = "Player_" ..player.UserId
	local data = AllValues
	
	local success, errorMessage = pcall(function()
		StatsData:SetAsync(playerUserId, data)
	end)

	if success then
		print("dataSaves")
	else
		print("Error")
		warn(errorMessage)
	end
end)
1 Like

Try this code

local DataStoreService = game:GetService("DataStoreService")
local StatsData = DataStoreService:GetDataStore("leaderStatsData")
local Players = game:GetService("Players")
local PlayerExperience={}
local AllValues = {
	LevelValue = nil,
	ManaValue = nil,
	StaminaValue = nil,
	HpValue = nil
}


game.Players.PlayerAdded:Connect(function(player)     
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 0
	Level.Parent = player
		
	local Mana = Instance.new("IntValue")
	Mana.Name = "Mana"
	Mana.Value = 0
	Mana.Parent = player
		
	local Stamina = Instance.new("IntValue")
	Stamina.Name = "Stamina"
	Stamina.Value = 0
	Stamina.Parent = player
		
	local Hp = Instance.new("IntValue")
	Hp.Name = "Hp"
	Hp.Value = 0
	Hp.Parent = player

	local playerUserId = "Player_" ..player.UserId

	local data
	local success, errorMessage = pcall(function()
		data = StatsData:GetAsync(playerUserId)
	end)
	
	if success  then
        PlayerExperience[player]=data or AllValues
	else
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local AllValues = {
		LevelValue = player.Level.Value,
		ManaValue = player.Mana.Value,
		StaminaValue = player.Stamina.Value,
		HpValue = player.Hp.Value
	}
	
	local playerUserId = "Player_" ..player.UserId
	local data = AllValues
	
	local success, errorMessage = pcall(function()
		StatsData:SetAsync(playerUserId, data)
	end)

	if success then
		print("dataSaves")
	else
		print("Error")
		warn(errorMessage)
	end
end)

Essentially i create a player dictionary called PLAYEREXPERIENCE to create information on players with the key being the player object and the value being the saved player stats dictionary or the default allvalues dictionary.

Additionally,i fixed a bug in your script that would occur later on that you may have trouble within your game being public as you messed up with variable scopes previously in the script you provided as the AllValues variable must be local scope in the PlayersRemoving function as having it global scope would alter with PlayerAdded Event for new players who have never played the game before with the previous player data being saved.

-- Types
type Service = DataStoreService | Players
type AllValuesType = {
	["LevelValue"]: number | nil,
	["ManaValue"]: number | nil,
	["StaminaValue"]: number | nil,
	["HpValue"]: number | nil
}

-- Services
local Services: {[string]: Service} = {
	DataStoreService = game:GetService("DataStoreService"),
	Players = game:GetService("Players")
}

-- This will be the stats of a new player
local StarterValues = {
	LevelValue = 1,
	ManaValue = 10,
	StaminaValue = 5,
	HpValue = 10
} :: AllValuesType

-- DataStore
local StatsData = Services.DataStoreService:GetDataStore("LeaderStatsData")

-- Functions
local function PlayerAdded(Player: Player)
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Parent = Player

	local Mana = Instance.new("IntValue")
	Mana.Name = "Mana"
	Mana.Parent = Player

	local Stamina = Instance.new("IntValue")
	Stamina.Name = "Stamina"
	Stamina.Parent = Player

	local Hp = Instance.new("IntValue")
	Hp.Name = "Hp"
	Hp.Parent = Player

	local Key = "Player_"..tostring(Player.UserId)

	local Data
	
	local Success, ErrorMessage = pcall(function()
		Data = StatsData:GetAsync(Key) :: AllValuesType
	end)

	if Success and not ErrorMessage then
		Level.Value = Data and Data.LevelValue or StarterValues.LevelValue
		Mana.Value = Data and Data.ManaValue or StarterValues.ManaValue
		Stamina.Value = Data and Data.StaminaValue or StarterValues.StaminaValue
		Hp.Value = Data and Data.HpValue or StarterValues.HpValue
	elseif not Success or ErrorMessage then
		warn(ErrorMessage)
	end
	
	Key = nil
	if Data then
		table.clear(Data)
		Data = nil
	end
end

local function PlayerRemoving(Player: Player)
	local Key = "Player_"..tostring(Player.UserId)
	
	local LastData
	
	local Success1, ErrorMessage1 = pcall(function()
		LastData = StatsData:GetAsync(Key) :: AllValuesType
	end)
	
	if not Success1 or ErrorMessage1 then
		print(ErrorMessage1)
	end
	
	local Data = {
		LevelValue = Player and Player:FindFirstChild("Level") and Player.Level.Value or (LastData and LastData.LevelValue or StarterValues.LevelValue),
		ManaValue = Player and Player:FindFirstChild("Mana") and Player.Mana.Value or (LastData and LastData.ManaValue or StarterValues.ManaValue),
		StaminaValue = Player and Player:FindFirstChild("Stamina") and Player.Stamina.Value or (LastData and LastData.StaminaValue or StarterValues.StaminaValue),
		HpValue = Player and Player:FindFirstChild("Hp") and Player.Hp.Value or (LastData and LastData.HpValue or StarterValues.HpValue)
	} :: AllValuesType

	local Success2, ErrorMessage2 = pcall(function()
		StatsData:SetAsync(Key, Data)
	end)

	if Success2 and not ErrorMessage2 then
		print("Data successfully saved!")
	elseif not Success2 or ErrorMessage2 then
		warn(ErrorMessage2)
	end
	
	Key = nil
	if LastData then
		table.clear(LastData)
		LastData = nil
	end
	if Data then
		table.clear(Data)
		Data = nil
	end
end

-- Connections
Services.Players.PlayerAdded:Connect(PlayerAdded)
Services.Players.PlayerRemoving:Connect(PlayerRemoving)

There will be maybe bugs because i wrote this fast

1 Like

thanks it works (sorry for late reply i was busy and i couldnt test the code)

I would recommend using ProfileStore - Save your player data easy (DataStore Module) - Resources / Community Resources - Developer Forum | Roblox