Healing script won't work

im making a healing area that heals 1 hp per second but for some reason the script doesn’t do anything when i touch the area.

local cooldown = false

script.Parent.HealRange.Touched:Connect(function(hit)
	if cooldown == false then
		if hit.Parent:FindFirstChild("Humanoid") then
			cooldown = true
			hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health + 1
			wait(1)
			cooldown = false
		end
	end
end)
2 Likes

cooldown is first initalized as false instead of true

1 Like

Please optimize the code and make it readable!

local db:boolean = false

script.Parent.HealRange.Touched:Connect(function(hit):()
	if db then return end
	local hum = hit.Parent:FindFirstChild("Humanoid")::Humanoid?
	if not hum then return end
	db = true
	hum.Health += 1
	task.wait(1)
	db = false	
end)
1 Like

that is actually less readable

2 Likes

You may want to use GetPartsInPart() instead in a loop.

https://create.roblox.com/docs/reference/engine/classes/WorldRoot#GetPartsInPart

It’d function similar to Touched, except you’d have to create a for loop that loops through all parts and checks for a character with a humanoid. (You could also set up a collision group to check only for players)

It looks like you want it to continually healing the people that are in there not just when it is touched.
The script you have right now will only run when it is touched

here is an updated version (this should work)
(the reason i am using this instead of for loop for partsinpart is so that if there is no cooldown then the instant someone touches it they will be healed)

local healrange = [part]

local charsinbox = {}

local function RunHealing()
    for char in charsinbox do
        if char ~= nil then
            char.Humanoid.Health += 1
        else
            table.remove(harsinbox,table.find(charsinbox,char))
        end
    end
end)

local function StartLoop()
    while wait(1) do
          if #charsinbox == 0 then
               return
          else
               RunHealing()
          end
    end)
end)

healrange.Touched:Connect(function(hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent) then
        table.insert(charsinbox,hit.Parent)
        if #charsinbox == 1 then
            StartLoop()
        end
    end
end)
healrange.TouchEnded:Connect(function(hit)
      if game.Players:GetPlayerFromCharacter(hit.Parent) then
        table.remove(charsinbox,table.find(charsinbox,hit.Parent))
    end
end)
1 Like

Thanks for your feedback!
Its very important to have a feedback from self proclaimed “programmer with 6 years of experience” but who is actually a member of devforum less than a year :handshake:
Based on your reply there is so much contribution given to the thread and solving of OP’s problem specifically :brain: :light_bulb:

1 Like
local Healing: {[Humanoid]: thread} = {}

local function StartHealing(hit: BasePart)
    local Character = hit:FindFirstAncestorOfClass("Model")
    local Humanoid = Character:FindFirstChildOfClass("Humanoid")
    
    if not Humanoid then
        return
    end
    
    if Healing[Humanoid] then
        return
    end
    
    Healing[Humanoid] = task.spawn(function()
        while true do
            task.wait(1)
            Humanoid.Health += 1
        end
    end)
end

local function EndHealing(hit: BasePart)
    local Character = hit:FindFirstAncestorOfClass("Model")
    local Humanoid = Character:FindFirstChildOfClass("Humanoid")

    if not Humanoid then
        return
    end
    
    local Thread = Healing[Humanoid]

    if not Thread then
        return
    end
    
    if coroutine.status(Thread) ~= "dead" then
        task.cancel(Thread)
    end
    
    Healing[Humanoid] = nil
end

HealRange.Touched:Connect(StartHealing)
HealRange.TouchEnded:Connect(EndHealing)

spoonfed, bye

this isn’t working for me, is it sopposed to be on a server script or a local script?

It is a server script, but a terribly inefficient way to go about it.

i put it in a server script tho and it does nothing

Try this script.

local heal : Part = script.Parent.HealRange

function getHumanoids()
	local humans : {Humanoid} = {}
	
	local parts = workspace:GetPartsInPart(heal)
	
	for i, part in pairs(parts) do
		if part.Parent:FindFirstChildWhichIsA("Humanoid")  and not table.find(humans, part.Parent:FindFirstChildWhichIsA("Humanoid")) then
			table.insert(humans,part.Parent:FindFirstChildWhichIsA("Humanoid"))
		end
	end
	return humans
end

while true do
	task.wait(1)
	local humans = getHumanoids()
	for i, human in pairs(humans) do
		human.Health += 1
	end
end

Additionally, more logic could be added, like making sure the humanoid’s health doesn’t go above their MaxHealth.

1 Like

You are saying this is more efficient? LOL

1 Like

Making a thread TO HEAL EACH HUMANOID LOL???

They are canceled if you didn’t see.

You are recomputing parts within heal part every second, iterating trough it with pairs, calling table.find, table.insert, FindFirstChildWhichisA every second.
Yours is 10000% worse

my code might have an efficiency of at worst n^2 for each additional humanoid. Thing is, roblox will crash and burn long before there is enough humanoids in this part’s bounds to cause any performance issue

This is horrid.

HealRange.Touched:Connect(StartHealing)
HealRange.TouchEnded:Connect(EndHealing)

This just puts the final nail in the coffin.

It is not? And stop crying about it, I don’t call it every second like you

WOW, this is so bad, the heal pad is a fast projectile, that’s why it’s bad to use .Touched :open_mouth:

this also doesn’t work, idk whats wrong but when i put this in the game it doesn’t work at all.
it doesn’t matter how bad the code is as long as it works

Okay so this script needs to be able to reference heal range.
Also could you show me what the heal range part itself looks like with a screenshot.