Hello, I have a script that is supposed to deal 8 damage every second. However, because the humanoid naturally heals some hp every second (I think), the script doesn’t really do anything. I’ve tried to change the wait value to 0.9 and 1.1, and that didn’t work either. Any thoughts?
Try
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
char:WaitForChild("Health").Disabled = true -- disables the healing script
end)
end)
Not working for me, I added it after the first if statement. (just the disabled line)
Put this in a brand new script. Not a if statement
I just put the char:WaitForChild(“Health”)… line after it. It theoretically should still work.
One of the best ways to resolve this problem would be to configure the Health script so you can enable and disable regeneration on demand. If you’re looking for a quick fix, you can use HealthChanged to prevent the Humanoid’s health from going up with the poison debuff enabled.
local lastHealth = Humanoid.Health
Humanoid.HealthChanged:Connect(function (newHealth)
if player.Assets.Poisoned.Value and newHealth > lastHealth then
Humanoid.Health = lastHealth
else
lastHealth = Humanoid.Health
end
-- Could instead set lastHealth here and remove elseif bit
end)
This code, or anything similar to it, will work. The idea is preventing the health from incrementing while the player is poisoned and if their attempted new health is higher than their current health.
For the most part, it works. However, the health constantly sky rockets (like 50 hp) at the end of each tick of the poison. I can send you a video in a second.
It’s mostly rough code, wouldn’t expect it to work well. There are a few adjustments that you may be able to make in that scenario. For example, you can connect and disconnect the event rather than leaving it a detached system.
Some different code:
--- Return us a disconnectable function
-- I think this is valid, modifying parameters directly.
local function blockHealthIncrements(health)
return Humanoid.HealthChanged:Connect(function (newHealth)
Humanoid.Health = math.min(newHealth, health)
health = Humanoid.Health
end)
end
--- ValueObject changes fire with the new value.
player.Assets.Poisoned.Changed:Connect(function (isPoisoned)
if isPoisoned == true then
local block = blockHealthIncrements(Humanoid.Health)
for _ = 1, 5, 1 do
-- Select the larger of two numbers
local newHealth = math.max(player.Character.Humanoid.Health - 8, 0)
player.Character.Humanoid.Health = newHealth
wait(1.1)
end
block:Disconnect()
end
end)
I’m not really expecting that you’re copying and pasting my code, rather using these as steps to understand how you can approach the problem and try to work around it using existing reading material and such.
Have you tried putting a script named “Health” in StarterPlayer.StarterCharacterScripts and putting this code in it:
script:Destroy()
I still want health to regenerate when the player isn’t poisoned.
At the end, it still boosts the health as if the player was never poisoned. (aka. 45 to 91 health) I honestly don’t know why. (The solution was to remove the return in blockHealthIncrements. Weird.)
Sure about that? I’d hope not.
In that case, you need some other way to disconnect the function. The way I have it set up, if you don’t manage that connection, it will cause a memory leak due to a hanging connection.
Something like this maybe can do it over:
local healthChanged
healthChanged = ... -- Connect here
return healthChanged
It happened to remove the leap in health from 45 to 90 at the end, but health never regenerated after. So not really. After some testing, if the function returns anything, the leap from 45 to 90 happens. Of course, it can’t disconnect, but at least the health doesn’t bug out. Would the only other way of disconnecting it be destroying the script?
The problem may lie that you are doing this in a local script.