How do i save humanoid properties with datastore?

Hello i am trying to use datastore to save a players humanoid properties such as Maxhealth, WalkSpeed etc. I have a script that i'm using to save tools in a players backpack which works fine. In that script i have tried adding another datastore to save the players current humanoid properties.

I have been trying to get it working all night and it basically acts like its saving the humanoid data but then when i relaod back into the game the humanoid.MaxHealth is back to its original 100hp. So it says its saving the MaxHealth data but does not actually save it.

If someone could look at my script and help me fix it i would be very greatful as i have been trying for the last 3 and a half hours to get it working. I have tried everything i can think of. But i just simply don’t understand how datastores work enough to finish the script

Like i said above i have alot of trouble understanding how datastores work so if the script looks weird sorry. i tried my best to understand. Also anything in the code relevant to the topic will have a comment next to it that says "--FOR HUMANOID DATA"

Here is the script...

local ds = game:GetService("DataStoreService")
local store = ds:GetDataStore("saveStore")
local store2 = ds:GetDataStore("saveMaxHealth") -- FOR HUMANOID DATA
local library = rs:WaitForChild("Weapons")

--< durectory function
	
local dir = {}

local function edit(player, list)
	dir[player.Name] = list -- {"tool", "tool2"}
end

local function setup(player, list)
	for i = 1, #list do
		local tool = library:FindFirstChild(list[i])
		if tool then
			local clone = tool:Clone()
			clone.Parent = player.Backpack
		else
			print(list[i] .. " not found")
		end
	end
end

--< player events

game.Players.PlayerAdded:Connect(function(player)
	local ready = false
	player.CharacterAdded:Connect(function(char)
		local bp = player.Backpack
		local data = nil
		local MaxHealthData = nil

		MaxHealthData = store2:GetAsync(player.UserId) -- FOR HUMANOID DATA
		
		if ready == false then
			ready = true
			
			data = store:GetAsync(player.UserId)
			
			if data then
				setup(player, data)
				edit(player, data)
			end
		end
		
		char.Humanoid.Died:Connect(function() --< lose gear on death
			char.Humanoid:UnequipTools()
			
			local old = player.StarterGear:GetChildren()
			for i = 1, #old do
				old[i]:Destroy()
			end
			local new = player.Backpack:GetChildren()
			for i = 1, #new do
				new[i].Parent = player.StarterGear
			end
		end)
		
		--< adjuster
		
		local count = 0
		local function adjust()
			if char.Humanoid.Health > 0 then
				local list = {}
				local equipped = char:FindFirstChildOfClass("Tool")
				if equipped then
					table.insert(list, equipped.Name)
				end
				
				local tools = bp:GetChildren()
				for i = 1, #tools do
					table.insert(list, tools[i].Name)
				end
				
				if count ~= #list then
					edit(player, list)
					count = #list
				end
			end
		end
		
		--< child events
		
		bp.ChildAdded:Connect(adjust)
		bp.ChildRemoved:Connect(adjust)
		
		char.ChildAdded:Connect(function(child)
			if child.ClassName == "Tools" then
				adjust()
			end
		end)
		char.ChildRemoved:Connect(function(child)
			if child.ClassName == "Tools" then
				adjust()
			end
		end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	store:SetAsync(player.UserId, dir[player.Name])
	dir[player.Name] = nil
end)

game.Players.PlayerRemoving:Connect(function(player) -- FOR HUMANOID DATA
	if player.Character then
		local humanoid = player.Character.Humanoid
		store2:SetAsync(player.UserId, humanoid.MaxHealth)
		print("player MaxHealth saved: "..tostring(humanoid.MaxHealth))
	end
end)

--< safety

game:BindToClose(function()
	wait(1)
end)```
1 Like

I am pretty sure the reason why you are unable to store those values into the DataStore is because the player already left. In this part:

game.Players.PlayerRemoving:Connect(function(player)
	if player.Character then
		local humanoid = player.Character.Humanoid
		store2:SetAsync(player.UserId, humanoid.MaxHealth)
		print("player MaxHealth saved: "..tostring(humanoid.MaxHealth))
	end
end)

The player has already left, so you would not be able to retrieve their humanoid’s values. Perhaps store it into a variable when they join, for example:

game.Players.PlayerAdded:Connect(function(player)
    local humanoidhealth
    while player do
        humanoidhealth = player.Character.Humanoid.Health -- Or MaxHealth, depending on your objective.
    end
    --Then save it to your DataStore every once in awhile.
end)

There may be an error somewhere else, but from what I can see; that is what needs to be changed.

1 Like

Its hard for me to believe that because if you run the game and add some MaxHealth then leave the game this is the output:

  Player Gold Data successfully saved
  Player Xp Data successfully saved
  player MaxHealth saved: 730 ```

but then if you join the game again and check your maxhealth it is set back to 100

Sorry if I missed something, but where is the part where is sets the player’s health to the value stored in the DataStore?

If your asking how am i adding more maxhealth im just using a part in game when you touch it it just gives you more maxhealth.

Not what I meant, perhaps I was unclear. You saved the MaxHealth value to the DataStore, but there should be another function:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAppearanceLoaded:Connect(function(character)
        local savedMaxHealth
        local success,fail = pcall(function() 
            savedMaxHealth = store2:GetAsync(player.UserId) 
        end)
        if success then
            player.Character.Humanoid.MaxHealth = savedMaxHealth
        end
        if fail then
            --Let the player know that their health has not loaded or something.
        end
    end)
end)

Or if I already missed the function similar to this, my apologies.

3 Likes

Okay i think i understand. Im missing this function so let me try to implement something like this and i will get back to you in a few minutes hopefully

Alright, good luck! Also depending on your game, make sure all assets have loaded before setting the player’s MaxHealth.

Okay i see what i did. Intsead of creating a new PlayerAdded function like you suggested. i just tried setting the value in my existing playeradded function meant for saving the tools. Anyway it works now thank you very much :smiley:

2 Likes