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)
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)
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)
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
Based on your reply there is so much contribution given to the thread and solving of OP’s problem specifically
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)
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.
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 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