so as the question says i would like the game to save the players health and give it to them on return which obviously means its datastore time (yes i will convert to datastore2 later) but with this script i made it says humanoid is not a nil value (line 21)and before i added workspace.name to line 20 it wouldnt say anything at all
script:
local Data = game:GetService("DataStoreService"):GetDataStore("Health")
game.Players.PlayerAdded:Connect(function(plr)
local Folder = Instance.new("Folder")
Folder.Name = "Health"
Folder.Parent = plr
local Health = Instance.new("IntValue")
Health.Name = "Health"
Health.Parent = Folder
print("Gave "..plr.Name.." Health Data")
local SavedItems = Data:GetAsync(plr.UserId)
if SavedItems then
Health.Value = SavedItems.Health
print(plr.Name.." had Health Data")
else
Health.Value = 100
print(plr.Name.." didn't have Health Data")
end
local name = plr.Name
local Character = workspace.name or plr.Character or plr.CharacterAdded:Wait()
Character.Humanoid.Health = Health.Value
Health.Changed:Connect(function()
Character.Humanoid.Health = Health.Value
Character.Humanoid.Changed:Connect(function()
Health = plr.Character.Humanoid.Health
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local Saving = {["Health"] = (plr.Health).Health.Value;
}
Data:SetAsync(plr.UserId, Saving)
print("Saved "..plr.Name.."'s Health Data")
end)

3 comments
u can do local Folder = Instance.new("Folder", plr) instead of Folder.Parent = plr
No, that causes performance issues. Don't listen to him. https://devforum.roblox.com/t/psa-dont-use-instance-new-with-parent-argument/30296
Additionally, it's deprecated.