Ad
Log in to vote
0

Why doesn't just this part of my code work? Do I need to use something else?

Asked by 16 days ago

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.

0
Tell me if I need to post the whole script, sorry for the inconvenience. boijuztgotrecked4 5 — 16d
0
Is Hit a custom function you made? Or is it first called there? Jay123abc2 13 — 16d
0
Yes, Hit is a function before this line of code. It's a function that damages the player. boijuztgotrecked4 5 — 16d
0
i dont see a problem here, can u show us the hit function? AreaRankDragon 0 — 16d
0
I've posted the whole script instead boijuztgotrecked4 5 — 16d

1 answer

Log in to vote
0
Answered by 16 days ago

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 
0
Are you trying to make a npc damage the player? imnotaguest1121 74 — 15d
0
Yes. I want the NPC to damage the player when a part called "DangerZone" touches the player. boijuztgotrecked4 5 — 15d
Ad

Answer this question