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)```