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)
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
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
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
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)
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
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
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