So, I have multiple parts within on model, I want to make a script that says that if ANY of those parts gets hits, then do a given function. I figured the best was to reduce lag is to have one script that says if any of these parts in the model gets hit, then do so rather then having a script in every part.
If I were to put the script into every part it would appear like this [Works]:
function onTouch(hit)
local human = hit.Parent:findFirstChild("Humanoid")
if (human ~= nil) then
while (human ~= nil) do
local s = wait(1)
local health = human.Health
if health > 0 and health < human.MaxHealth then
local newHealthDelta = 0.01 * s * human.MaxHealth
health = health + newHealthDelta
human.Health = math.min(health,human.MaxHealth)
end
end
if human.Health > human.MaxHealth then
human.Health = human.MaxHealth
end
end
end
connection = script.Parent.Touched:connect(onTouch)
However, in my attempt to make it work by just putting one script in the model that access all parts, I think I am doing something wrong...
script.Parent:GetChildren()
function onTouch(hit)
local human = hit.Parent:findFirstChild("Humanoid")
if (human ~= nil) then
while (human ~= nil) do
local s = wait(1)
local health = human.Health
if health > 0 and health < human.MaxHealth then
local newHealthDelta = 0.01 * s * human.MaxHealth
health = health + newHealthDelta
human.Health = math.min(health,human.MaxHealth)
end
end
if human.Health > human.MaxHealth then
human.Health = human.MaxHealth
end
end
end
connection = script.Parent:GetChildren().Touched:connect(onTouch)
Can someone help me with what I am doing wrong please? I think I learned how to do this wrong seeing how I taught myself...

2 comments
Ah I see. Thanks!
Yowelcome.