Player not regening

Hello!

So I made a system in my game which is supposed to heal you every second if it’s enabled.
But the problem is that it doesn’t regen me.


Code :

-- Services

local RS = game:GetService("ReplicatedStorage")
local TS = game:GetService("TweenService")

------------------------------------------

-- Variables

local Remote = RS:WaitForChild("ToggleSkill2")

local RegenAmount = 10
local RegenCooldown = 2

------------------------------------------

Remote.OnServerEvent:Connect(function(player, isOn, buttonColor)
	
	local character = player.Character
	local humanoid = character.Humanoid
	local health = humanoid.Health
	local maxHealth = humanoid.MaxHealth
	
	local function ErmitMode()
		while wait(RegenCooldown) do
			if isOn == true then
				health += RegenAmount
			end
		end
	end
	
	if isOn == false then
		TS:Create(buttonColor, TweenInfo.new(0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {ImageColor3 = Color3.fromRGB(255,0,0)}):Play()
	end
	if isOn == true then
		TS:Create(buttonColor, TweenInfo.new(0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {ImageColor3 = Color3.fromRGB(0,255,0)}):Play()
	end
	
	ErmitMode()
	
end)

Thanks in advance!

You’re changing the value of a variable and not the property, increment humanoid.health rather than health

And the way you have the code set up is going to lead to problems since you’d have no way to turn off the healing when needed, you may also need to resolve that issue as well, my only idea is with a dictionary that stores who currently has healing turned on

1 Like

Can you show an example of how to make it turn off?

An example I probably have would be an array where you store the players that currently need to have health regenerated, and when you want it off via calling the remote event and making isOn false, remove the player from the array.

And have the if statement check if the player is still in the array, and if they aren’t, stop the loop

1 Like

Okay if I understand what you’re saying correctly :

  • Put player in a table when isOn = true
  • Heal the players who are in the table
  • Removing the player from the table when isOn = false

That is correct, All you really need to do is remove the player in the if isOn == false then if statement, add then in the isOn == true statement and make the condition in the loop check if the player is in the still in the table

For the table, I recommend store the player’s UserId since you can’t change that and it’s what most people do, make sure to put the ErmitMode call in the true condition

Oh and, as a recommendation, you don’t need == true and == false, you can remove the == true and for false checking, put a not behind the boolean you’re checking

if isOn then
if not isOn then

1 Like

I did this right now :

Remote.OnServerEvent:Connect(function(player, isOn, buttonColor)
	
	local character = player.Character
	local humanoid = character.Humanoid
	
	local PlayersHealing = {}
	
	local function ErmitMode()
		while wait(RegenCooldown) do
			if table.find(PlayersHealing, player) then
				player.Character.Humanoid.Health += RegenAmount
			else
				print("Ermit Mode disabled, not regening.")
			end
		end
	end
	
	if isOn == false then
		TS:Create(buttonColor, TweenInfo.new(0.15, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {ImageColor3 = Color3.fromRGB(255,0,0)}):Play()
		table.remove(PlayersHealing, player)
	end
	if isOn == true then
		TS:Create(buttonColor, TweenInfo.new(0.15, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {ImageColor3 = Color3.fromRGB(0,255,0)}):Play()
		table.insert(PlayersHealing, player)
	end
	
	ErmitMode()
	
end)

PlayersHealing should be outside of the OnServerEvent so it doesn’t reset

Again, I would recommend storing the UserId rather than the Player object since it’s less prone to issues since it’s a number rather than an object

And you’re close on the loop, though if the player is not in the table anymore, break the loop so it stops repeating

But you also need to keep into account if the player’s character still exists, so have it in the while condition

while player.Character and task.wait(RegenCooldown) do

So that way if the player’s character doesn’t exist anymore you wont get an error

1 Like

Then how can I access the humanoid if I have the UserId ?

You’re just checking if their id exists in the table, and removing/adding ids, you already have the humanoid since OnServerEvent has player, which can give Character and as a result humanoid

1 Like

I currently have this code. But it still somehow regens me I disable it after enabling it.

-- Services

local RS = game:GetService("ReplicatedStorage")
local TS = game:GetService("TweenService")

------------------------------------------

-- Variables

local Remote = RS:WaitForChild("ToggleSkill2")

local RegenAmount = 10
local RegenCooldown = 2

local PlayersHealing = {}

------------------------------------------

-- Code

Remote.OnServerEvent:Connect(function(player, isOn, buttonColor)
	
	local character = player.Character
	local humanoid = character.Humanoid
	
	local function ErmitMode()
		while player.Character and task.wait(RegenCooldown) do
			if table.find(PlayersHealing, player.UserId) then
				player.Character.Humanoid.Health += RegenAmount
			end
			if not table.find(PlayersHealing, player.UserId) then
				break
			end
		end
	end
	
	if isOn == false then
		TS:Create(buttonColor, TweenInfo.new(0.15, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {ImageColor3 = Color3.fromRGB(255,0,0)}):Play()
		table.remove(PlayersHealing, player.UserId)
	end
	if isOn == true then
		TS:Create(buttonColor, TweenInfo.new(0.15, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {ImageColor3 = Color3.fromRGB(0,255,0)}):Play()
		table.insert(PlayersHealing, player.UserId)
	end
	
	ErmitMode()
	
end)

You’re using table.remove incorrectly in this case, the 2nd parameter expects you to give the index of what you want to remove, in your case you’d need to do

table.remove(PlayersHealing, table.find(PlayersHealing, player.UserId))

And I’d recommend putting ErmitMode() in the isOn == true statement as from my testing, not putting it in still caused issues even with the change mentioned above

1 Like

Thank you so much it worked :blush:

You just saved my freaking life :smiley:

1 Like