Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can i trigger my npc to move when a part is touched?

Asked by 6 years ago

Hello I am very new to scripting, and I mostly edit scripts. This is because I can never really find what I need to know on the wiki or other places. Or the script's no longer work.

I have this walking npc script that I edited quite a bit

model = script.Parent.Parent
hum = script.Parent.Humanoid
torso = script.Parent.Torso
while true do
    if model.PointA ~= nil then
        a = model.PointA
        hum:MoveTo(a.Position, a)
        wait(10)
            wait(0.1)
        until (a.Position - torso.Position).magnitude <= 5
    else
        print("Error 404 Point A not found.")
    end
wait(0.1)
    if model.PointB ~= nil then
        b = model.PointB
        hum:MoveTo(b.Position, b)
        wait(1.5)
            wait(0.1)
        until (b.Position - torso.Position).magnitude <= 5
    else
        print("Error 404 Point B not found.")
    end

end

I'd like to make a separate script that has debounce and triggers that script to be enabled, wait a few seconds, than becomes disabled again in just enough time for the npc to make its run.

Any help would be greatly appreciated! Thanks :)

1 answer

Log in to vote
0
Answered by
Ghusoc 50
6 years ago
Edited 6 years ago

First things first, you're going to want to know about the Touched event. It has one parameter, which is the part that hit the part being triggered. In other words:

thePartBeingTouched.Touched:Connect(function(thePartTouching)

end)

Because the event has a habit of firing rapidly in succession, we want to add a debounce.

local wasTouched = false

part.Touched:Connect(function(hit)
    if not wasTouched then
        wasTouched = true

        wait(1)
        wasTouched = false
    end
end)

Next, you want to check whether the part that touched is a descendant of the player's character (in other words, check if the player touched the part). To do this, we'll check whether the part's parent has a humanoid.

local wasTouched = false

part.Touched:Connect(function(hit)
    if not wasTouched then
        wasTouched = true
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then

        end
        wait(1)
        wasTouched = false
    end
end)

Now, we can then activate the script by disabling the Disabled property.

local wasTouched = false
local npcScript = path -- replace with a path to the npc script.

part.Touched:Connect(function(hit)
    if not wasTouched then
        wasTouched = true
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            npcScript.Disabled = false
        end
        wait(1)
        wasTouched = false
    end
end)

And if everything is done accordingly, the NPC script should perform as expected upon a player touching the part.

0
Thanks so much EtherealTrin 45 — 6y
Ad

Answer this question