How can i save humanoid Data with Database?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? i want to save humanoid data like walkspeed and health

  2. What is the issue? the data wont save if i die or rejoin

  3. What solutions have you tried so far? using player.characterAppareanceLoaded and WaitForChild but nothing

also, i dont get any error and everything works fine, it just doesnt save humanoid proprietes.

local plrs = game:GetService("Players")
local dataServ = game:GetService("DataStoreService")
local sessionData = {}

local Database = dataServ:GetDataStore("HealthData")



function PlayerAdded(player)
    local character = player.Character or player.CharacterAdded:Wait()
    local hum = character:WaitForChild("Humanoid")
    local success = nil
    local playerData = nil
    local attempt = 1
    
    repeat
        success, playerData = pcall(function()
            return Database:GetAsync(player.UserId)
        end)
        attempt += 1
        if not success then
            warn(playerData)
            task.wait(3)
        end

    until success or attempt == 5

    if success then
        print("connected to Database ")
        if not playerData then
            print("assigned default data")
            playerData = {
                ["MaxHealth"] = 20,
                ["WalkSpeed"] = 16
            }
        end
        sessionData[player.UserId] = playerData
        else
            warn("Unable to get data")
            player:Kick("Unable to find your data, please try again later")
    end

end







    

plrs.PlayerAdded:Connect(PlayerAdded)



function PlayerLeaving(player)
    if sessionData[player.UserId] then --getting error message here
        local success = nil
        local errorMsg = nil
        local attempt = 1
        
        repeat
        success, errorMsg = pcall(function()
                Database:SetAsync(player.UserId, sessionData[player.UserId])
            end)
            attempt += 1
            if not success then
                warn(errorMsg)
                task.wait(3)
            end
            
        until success or attempt == 5
        
        if success then
            print("Data saved for ", player.Name)
        else
            print("Unable to save", player.Name )
        end
        
    end
end

plrs.PlayerRemoving:Connect(PlayerLeaving)

thanks for your time, if you have any new script for this i will replace this

4 Likes