Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Help With Saving MaxHealth Using DataStores?

Ive been trying to save each seperate players "MaxHealth" So they can rejoin the server/die and get back their max health I've tried multiple scripts so I was wondering how I would fix it I would expect this script to work but sadly it does not D:

local DataStore = game:GetService("DataStoreService")
local ds3 = DataStore:GetDataStore("MaxHealthSaveSystem")

game.Players.PlayerAdded:connect(function(player)	
	local maxhealth = Instance.new("IntValue",player)
	maxhealth.Name = "MaxHealth"
	local forever = 2
	
	repeat 
	local botty = workspace:WaitForChild(player.Name).Humanoid.MaxHealth
	maxhealth.Value = botty
	until forever == 2
	maxhealth.Value = ds3:GetAsync(player.UserId) or 0
	ds3:SetAsync(player.UserId, maxhealth.Value)
	maxhealth.Changed:connect(function()
		ds3:SetAsync(player.UserId, maxhealth.Value)
	end)

end)

game.Players.PlayerRemoving:connect(function(player)
	ds3:SetAsync(player.UserId, player.leaderstats.MaxHealth.Value)
	
end)

1 answer

I think you either forgot about creating leaderstats or you forgot about making maxhealth's parent leaderstats:

local DataStore = game:GetService("DataStoreService")
local ds3 = DataStore:GetDataStore("MaxHealthSaveSystem")

game.Players.PlayerAdded:connect(function(player)
	local stats = Instance.new("IntValue",player) --creates the list of gathered values
	stats.Name = "leaderstats"
	local maxhealth = Instance.new("IntValue",stats) --creates the value that is going to be saved/loaded
	maxhealth.Name = "MaxHealth"
	
	local key = player.userId
	local savedValues = ds3:GetAsync(key)
	
	if savedValues then --this will check if player's value has been saved previously
		maxhealth.Value = savedValues[1]
	else
		ds3:SetAsync(key, maxhealth.Value)
	end
end)

end)

game.Players.PlayerRemoving:connect(function(player)
    ds3:SetAsync(player.UserId, player.leaderstats.MaxHealth.Value)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.