hello i was having a problem on making a healing system with a heal delay my script was on StaterCharacterScripts and its a local scripts i triend every thing i know and im a new at scripting so can someone help me,here is the script:
local character = game.Players.LocalPlayer.Character
local Humanoid = character.Humanoid or character:WaitForChild("Humanoid")
local hp = Humanoid.Health
local maxhp = Humanoid.MaxHealth
local tme = 3
Humanoid.HealthChanged:connect(function()
print("health Changed")
while true do
if tme == 3 then
print("starting")
repeat
tme = tme - 1
wait(1)
until tme == 0
if tme == 0 then
print("healing")
repeat
hp = hp + 5
wait(5)
until hp == maxhp
end
end
end
end)
just reply me if i have a mistake and help me pls…
it will be a big help to me =)
A few things to say about this, first @codyorr4 is right, you are storing a health value from the humanoid, so it doesn’t match up exactly with your humanoids actual health. Make sure to change it to the health they have at that moment.
Second what is the point of having this “tme” variable if you just want it to stall for 3 seconds? I would get rid of that whole if section and replace it with wait(3). I would also not use connect since it is depreciated, use Connect when creating a function. finally you don’t need a while loop in there if you are searching for when their health changes, it will just lead to a script timeout.
After this I was able to shorten your code like so:
local character = game.Players.LocalPlayer.Character
local Humanoid = character:WaitForChild("Humanoid")
local maxhp = Humanoid.MaxHealth
Humanoid.HealthChanged:Connect(function()
print("health Changed")
wait(3)
print("healing")
repeat
Humanoid.Health = Humanoid.Health + 5
wait(5)
until hp >= maxhp
end)
Edit: Extra note, as long as you don’t change the max health in your humanoid, this should work perfectly fine. If you do change your humanoid’s max health, define it like I did with the health of the humanoid here.
if you encounter this not stopping the healing loop, you should change the
until hp == maxhp
to
until hp >= maxhp
this is so if you get a number that is not even with 5, let’s say 27. try adding 5 onto 27 until you reach 100, you won’t reach a perfect 100, something like 102, and because you defined the operator as “is equal to”, the script will not stop healing unless the health is equal to 100.
the humanoids health can’t go past the maxhealth because of literally the word “max” and how roblox programmed it. Either way would work though and it is good to know this just in general, thanks for pointing that out I did change it.
Well, let me understand: You are creating a script that knows when the health is changed, and in some seconds later, your character heals. Well, roblox provides you one, here is the script, i found it in Roblox Studio data: (This code below doesn’t works, the one that works is the other provided at the bottom of this post.)
<roblox xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.roblox.com/roblox.xsd" version="4">
<External>null</External>
<External>nil</External>
<Item class="Script" referent="RBX0">
<Properties>
<bool name="Disabled">false</bool>
<Content name="LinkedSource"><null></null></Content>
<string name="Name">Health</string>
<string name="ScriptGuid">{EC3A881D-5F49-4644-A69D-FB60F2E59FF2}</string>
<ProtectedString name="Source"><![CDATA[-- 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]]></ProtectedString>
</Properties>
</Item>
</roblox>
By the way, that’s the original code, here i will give you the code that works:
-- 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