So...in my script what I have is a model with several parts and a script. This script is suppose to make it so that if any of these parts gets hit, then the following function will occur. The output error I am getting is as follows...
23:06:43.724 - Touched is not a valid member of Script 23:06:43.724 - Script 'Workspace.Model.Script', Line 21 23:06:43.724 - Stack End
Here is the script...
for i, child in pairs (script.Parent:GetChildren()) do if child:IsA("BasePart") then -- consider only the Parts of the model 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 end connection = child.Touched:connect(onTouch) end
Your line 21 is not included in the check that child:IsA("BasePart")
; so you have a Script which is a child
, but Script objects do not have a Touched
property.
Move line 21 into the condition to make sure it is a BasePart, ensuring it has a Touched
property.
You should really retab your code, too.
for i, child in pairs (script.Parent:GetChildren()) do if child:IsA("BasePart") then -- consider only the Parts of the model 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 = child.Touched:connect(onTouch) end end
Note that the assignment to connection
(on your line 21, my line 20) is redundant since it would be overridden repeatedly by the loop. Since you aren't using it, you shouldn't include it in your code - it could be confusing.
Like in good writing, every element of a script should have a clear purpose.