(still need help) Cant find a work around for my health datastore

I want a datastore to save your health so when people are about to die, they won’t chicken out and leave the game so they wont risk losing their in game money, because my health script saves their health when rejoined or joined, so theres no way to save themselves. they will lose their progress either way

  1. Its not saving or loading to the player. and when joined it always give them 100 health.
  2. I have little knowledge of coding in lua, so i find workarounds in marketplace scripts on roblox.
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("HealthSave")

game.Players.PlayerAdded:connect(function(player)
player.Character.Humanoid.Health = ds:GetAsync(player.UserId)
ds:SetAsync(player.UserId, player.Character.Humanoid.Health)
player.Character.Humanoid.Health.Changed:connect(function()
ds:SetAsync(player.UserId, player.Character.Humanoid.Health)
end)
end)

3 Likes

Whats the point of saving right after you just loaded? The data will be exactly the same.

And saving every time the health changes will lead to so many throttled requests when the player is healing

You should be using a pcall function. If you use a pcall function you can then check what error you are getting. Although the pcall should not be causing the issue if you use a pcall function then it should give you the error/issue.

Somthing I also have noticed is that your saving the health twice for some reason. You should just be able to save it once not need to do it more then once.

If you don’t know what pcall functions are or how to use this this tutorial is very useful: Pcalls - When and how to use them

Also to add onto the reply that I wrote like what @DarkDanny04 said you should not keep saving the player health cuz it would sent many requests to the Datastore which would make it go over the rate limit and will throttle. You can just make it so it will save at the end cuz you want it so if they leave it will save there health.

I have not tried this code but this is the kinda idea you want:

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("HealthSave")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local success, errorMessage = pcall(function()
			char.Humanoid.Health = ds:GetAsync(player.UserId)
		end)
		if not success then
			print(errorMessage)
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	player.CharacterRemoving:Connect(function(char)
		local success, errorMessage = pcall(function()
			ds:SetAsync(player.UserId, char.Humanoid.Heath)
		end)

		if not success then
			print(errorMessage)
		end
	end)
end)
1 Like

I understand what your saying but I cant find the error message in the console/output and the code isnt working. i checked local logs and server logs and none of them currently have a message.

I just realized that my script was disabled from my plugins. and API access for studio was disabled. SummerLife gave me a code. The script seems to work, but always sets me to 0 health. a little roadblock. this is my code for reference

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("HealthSave")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local success, errorMessage = pcall(function()
			char.Humanoid.Health = ds:GetAsync(player.UserId)
			if char.Humanoid.Health == 0 then
				ds:SetAsync(player.UserId, 100)
			end
		end)
		if not success then
			print(errorMessage)
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	player.CharacterRemoving:Connect(function(char)
		local success, errorMessage = pcall(function()
			ds:SetAsync(player.UserId, char.Humanoid.Heath)
		end)

		if not success then
			print(errorMessage)
		end
	end)
end)

Avoid making multiple threads on the same topic.

Here’s a solution if anyone needs it.

1 Like