I have not modified the Health script in starter character.
We have 2 things, a damage brick and a problematic damage brick.
cool damage brick
Damages the player when touched.
Uses :TakeDamage()
Health Regen works as intended after damage is taken.
problematic damage brick
Damages the player when player can see it.
Uses Tween Service to deal damage.
Health Regen doesn’t work as intended after damage is taken.
Here’s the part of the script where damage is being dealt.
local character = script.Parent.Parent.Character
local humanoid = character:FindFirstChild("Humanoid")
local initialHealth = humanoid.Health
local targetProperties = { Health = humanoid.Health - damage.Value }
local tweenInfo = TweenInfo.new(tweenDuration, easingStyle, easingDirection)
local tween = TweenService:Create(humanoid, tweenInfo, targetProperties)
tween:Play()
Does tween service break health regen? Is there an alternative way to deal damage smoothly?
It decreases the health point and makes this easing effect. Also I can set a time length to change how fast the health decreases, it just feels easier.
The only alternative way is to repeatedly call :TakeDamage() or modify the humanoid health.
Typically it’s not recommended to run TweenService on the server side because of its high costs in updating the change to all of the clients in the game. But, if your game is small and it doesn’t cause any unwanted behaviors you should be fine with using it.
local REGEN_RATE = 1 / 100 -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 1 -- Wait this long between each regeneration step.
local LastHealth = Humanoid.Health
local DebHealth = true
local function Regen()
while Humanoid.Health < Humanoid.MaxHealth and Humanoid.Health > 0 and Humanoid:GetState() ~= Enum.HumanoidStateType.Dead do
local dt = wait(REGEN_STEP)
local dh = dt * REGEN_RATE * Humanoid.MaxHealth
Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
end
DebHealth = true
end
local function OnHealthChanged(NewHealth)
if NewHealth < LastHealth and DebHealth then -- Took Damage
DebHealth = false
task.spawn(Regen)
elseif NewHealth == Humanoid.MaxHealth then
print("Max Health!")
end
LastHealth = NewHealth
end
Humanoid.HealthChanged:Connect(OnHealthChanged)
thanks for the reply, I changed the default regen with this script (also linked the var Humanoid to the character’s humanoid). But it doesn’t work I will take a deep look and try to evolve your creation.
Yeah, It’s a local script. But I can’t use a server script to check if player can see a part. (there’s a proximity prompt in the damage part, I check if it’s shown to player, that’s how I know if someone’s looking at it.)
you should use RemoteEvent:FireServer() to start the health damaging on a serverscript.
:TakeDamaged() works on a localscript but it doesn’t replicate to all players and it doesn’t notice your health differentiation in the “Health” script.
Thanks. I seperated my script into a server and a local one, local script fires a remote event to the server to trigger the damage process. so server does the :TakeDamage part. Thank you again.