I'm trying to make an enemy NPC script, everything else in the script works except this part of the code:
for _, Child in pairs(script.Parent:GetChildren()) do if Child:FindFirstChild("DangerZone") then Child.Touched:connect(Hit) print("Player has been hit") end end
I'm still learning how GetChildren works, so sorry if I'm using it wrong or if I need to use something else.
Whole script because I've been asked to show the Hit function.
wait(1.5) local sp = script.Parent local Humanoid = sp:WaitForChild("Enemy") local SoundPlace = sp:WaitForChild("Head") local Animation = script:WaitForChild("AttackAnim") local Yeti = Instance.new("Sound",SoundPlace) Yeti.Volume = 1 Yeti.PlaybackSpeed = 1.2 Yeti.SoundId = "http://www.roblox.com/Asset?id=9114081952" local AttackEnabled = true local function wait(TimeToWait) if TimeToWait ~= nil then local TotalTime = 0 TotalTime = TotalTime + game:GetService("RunService").Heartbeat:wait() while TotalTime < TimeToWait do TotalTime = TotalTime + game:GetService("RunService").Heartbeat:wait() end else game:GetService("RunService").Heartbeat:wait() end end function DamageTag(parent,damage) local DmgTag = script.DamageTag:clone() DmgTag.Damage.Value = damage DmgTag.creator.Value = game.Players.LocalPlayer DmgTag.Disabled = false DmgTag.Parent = parent end local Anim = Humanoid:LoadAnimation(Animation) function Hit(hit) local TargetHum = hit.Parent:FindFirstChild("Humanoid") if TargetHum ~= nil and TargetHum:IsA("Humanoid") and (TargetHum.Health > 1) and AttackEnabled == true and TargetHum ~= Humanoid then AttackEnabled = false delay(2,function() AttackEnabled = true end) if Anim then Anim:Play(nil,nil,1.1) end script.Parent.HumanoidRootPart.Anchored = false wait(0.1) DamageTag(TargetHum.Parent,47) Yeti:Play() wait(2) script.Parent.HumanoidRootPart.Anchored = false end end for _, Child in pairs(script.Parent:GetChildren()) do if Child:FindFirstChild("DangerZone") then Child.Touched:connect(Hit) print("Player has been hit") end end