I want to regenerate the characters health every second until they reach their maximum health but when I tried it failed, does anyone know how to fix this?
Can someone help or not, I really need to fix this.
Consider looking at the roblox docs page: https://create.roblox.com/docs/reference/engine/classes/Humanoid#Health
" Regeneration
If there is no Script named “Health” within StarterCharacterScripts, a passive health regeneration script is automatically inserted. This causes players’ characters to spawn with the same health regeneration script, which adds 1% of MaxHealth to Health each second, while the Humanoid is not dead. To disable this health regeneration behavior, add an empty Script named “Health” to StarterCharacterScripts. "
local Players = game:GetService("Players")
local regenDelta = 5 --// Health increase per second
Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local maxHealth = humanoid.MaxHealth
while player.Character do
humanoid.Health = math.min(humanoid.Health + regenDelta, maxHealth)
task.wait(1)
end
end)
Also (I provided a script bcuz it was pretty ez)
Roblox’s default script:
-- Gradually regenerates the Humanoid's Health over time.
local REGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 1 -- Wait this long between each regeneration step.
--------------------------------------------------------------------------------
local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'
--------------------------------------------------------------------------------
while true do
while Humanoid.Health < Humanoid.MaxHealth do
local dt = wait(REGEN_STEP)
local dh = dt*REGEN_RATE*Humanoid.MaxHealth
Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
end
Humanoid.HealthChanged:Wait()
end
Well I think this is the solution. Your script is correct but I want to also make some values and things inside of lighting destroy for the client when the loop is finished, how would I go about doing that?
If you want to run code once the loop ends/when character dies you can place it after the while since the while loop will prevent execution of code after it until it breaks
So like this?
local Regeneration = 5
local MaximumHealth = HumanoidCharacterTarget.MaxHealth
while VictimCharacter do
if VictimCharacter:FindFirstChild("Corpse") and VictimCharacter:FindFirstChild("Ghost") then
VictimCharacter:FindFirstChild("Corpse"):Destroy()
VictimCharacter:FindFirstChild("Ghost"):Destroy()
end
local Revived = Instance.new("ObjectValue")
Revived.Parent = VictimCharacter
Revived.Name = "Revived"
HumanoidCharacterTarget.Health = math.min(HumanoidCharacterTarget.Health + Regeneration, MaximumHealth)
task.wait(1)
end
You are creating an ObjectValue every time the loop runs, place it outside the loop
Okay so like this?
while VictimCharacter do
HumanoidCharacterTarget.Health = math.min(HumanoidCharacterTarget.Health + Regeneration, MaximumHealth)
task.wait(1)
end
if VictimCharacter:FindFirstChild("Corpse") and VictimCharacter:FindFirstChild("Ghost") then
VictimCharacter:FindFirstChild("Corpse"):Destroy()
VictimCharacter:FindFirstChild("Ghost"):Destroy()
end
local Revived = Instance.new("ObjectValue")
Revived.Parent = VictimCharacter
Revived.Name = "Revived"
end
Also if you don’t mind could you say what you are tryna achieve?
Basically I am trying to make an revival ability, I already setup a system for when the character dies and they press a button they’d basically just sit there and see a ghostly atmosphere/background until someone uses the revival ability on them and they’d regenerate to full health and once they are at full health the lighting destroys along with the values that’s required for the ability to work.
Would this even work by the way? Because If I am not wrong when you die your humanoid goes into died mode so wouldn’t I need to change the mode once the loop is finished or? Also would destroying just that players lighting work within the script (the script is inside of a ModuleScript)?
Oh ryt in that case the code should be something like this:
local Revived = Instance.new("ObjectValue")
Revived.Parent = VictimCharacter
Revived.Name = "Revived"
while VictimCharacter and HumanoidCharacterTarget.Health < MaximumHealth do
HumanoidCharacterTarget.Health = math.min(HumanoidCharacterTarget.Health + Regeneration, MaximumHealth)
task.wait(1)
end
if VictimCharacter:FindFirstChild("Corpse") and VictimCharacter:FindFirstChild("Ghost") then
VictimCharacter:FindFirstChild("Corpse"):Destroy()
VictimCharacter:FindFirstChild("Ghost"):Destroy()
end
Thanks a lot for your help, can you answer this question though?
Would this even work by the way? Because If I am not wrong when you die your humanoid goes into died mode so wouldn’t I need to change the mode once the loop is finished or? Also would destroying just that players lighting work within the script (the script is inside of a ModuleScript)?
No lighting can only be changed from client scripts
In that case you need to call the while loop once the revival ability has been triggered
Well, you never hinted that this system would work after the character dies. In that case you can turn CharacteAutoLoads
off (but then you have to clone ui stuff; its a hassle) or tp the character to somewhere else and run the while loop after checking if the player died or not.
Okay well is there a way to run a LocalScript forever and make it so if the Revived value is found then it’d destroy the lighting that I want destroyed?
Yep, here’s how:
local LocalPlayer = Players.Character
local Character = LocalPlayer.Character
Character.ChildAdded:Connect(function(child)
if child:IsA("ObjectValue") and child.Name == "Revived" then
--// Destroy lighting
end
end)
And where would I place the script? Like what location.
Client side (StarterPlayerScripts) cuz afaik lighting is only accessible from client side