What do you want to achieve?
I want to make npc which teleport to some and check if player close door then players in a room will not die.
What is the issue?
As i think I need to make some invisible part and npc detect players in this part “killarea” and kill them. But idk how to make this.
local npc = workspace:WaitForChild("animatronic")
local teleportPoint = workspace:WaitForChild("endpoint")
local startpoint = workspace:WaitForChild("startpoint")
local doorState = workspace:WaitForChild("TestDoor"):WaitForChild("value")
local function killAllPlayers()
for _, player in ipairs(game.Players:GetPlayers()) do
local humanoid = player.Character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
end
local function npcBehavior()
while true do
npc.HumanoidRootPart.CFrame = teleportPoint.CFrame
task.wait(3)
if doorState.Value == "open" then
killAllPlayers()
npc.HumanoidRootPart.CFrame = startpoint.CFrame
else
npc.HumanoidRootPart.CFrame = startpoint.CFrame
print("Door Closed")
end
task.wait(5)
end
end
npcBehavior()
local npc = workspace:WaitForChild("animatronic")
local teleportPoint = workspace:WaitForChild("endpoint")
local startpoint = workspace:WaitForChild("startpoint")
local doorState = workspace:WaitForChild("TestDoor"):WaitForChild("value")
local killArea = workspace:WaitForChild("KillArea") -- Invisible part representing the kill area
-- Function to kill all players in the kill area
local function killPlayersInArea()
local touchingParts = killArea:GetTouchingParts()
for _, part in ipairs(touchingParts) do
if part:IsA("BasePart") and part.Parent:FindFirstChildWhichIsA("Humanoid") then
part.Parent.Humanoid.Health = 0
end
end
end
-- NPC behavior function
local function npcBehavior()
while true do
npc.HumanoidRootPart.CFrame = teleportPoint.CFrame
task.wait(3)
if doorState.Value == "open" then
killPlayersInArea()
npc.HumanoidRootPart.CFrame = startpoint.CFrame
else
npc.HumanoidRootPart.CFrame = startpoint.CFrame
print("Door Closed")
end
task.wait(5)
end
end
npcBehavior()
Make sure the KillArea part is an invisible, non-collidable part that covers the room you want to check for players. The killPlayersInArea function will only affect players whose HumanoidRootPart is touching the KillArea part.
Remember to set up the KillArea part in your workspace and adjust its size and position to cover the desired room. Also, ensure that the KillArea part has CanCollide set to false and is transparent to make it invisible.
I think the reason the code doesn’t work is because :GetTouchingParts() only works if the part has CanCollide enabled. A simple workaround can be to use :GetPartBoundsInBox, which works when CanCollide is disabled. Here’s an updated script:
local npc = workspace:WaitForChild("animatronic")
local teleportPoint = workspace:WaitForChild("endpoint")
local startpoint = workspace:WaitForChild("startpoint")
local doorState = workspace:WaitForChild("TestDoor"):WaitForChild("value")
local killArea = workspace:WaitForChild("KillArea") -- Invisible part representing the kill area
local FilterObjects = {npc, killarea}
local BoxPosition = CFrame.new(0, 0, 0) -- set these to your box's position and size
local BoxSize = Vector3.new(0, 0, 0)
local MaxObjectsAllowed = 0
local BoxParams = OverlapParams.new(FilterObjects, Enum.RaycastFilterType.Exclude, MaxObjectsAllowed, "Default")
-- Function to kill all players in the kill area
local function killPlayersInArea()
local ObjectsInSpace = game.Workspace:GetPartBoundsInBox(BoxPosition, BoxSize, BoxParams)
for _, part in ipairs(ObjectsInSpace) do
if part:IsA("BasePart") and part.Parent:FindFirstChildWhichIsA("Humanoid") then
part.Parent.Humanoid.Health = 0
end
end
end
-- NPC behavior function
local function npcBehavior()
while true do
npc.HumanoidRootPart.CFrame = teleportPoint.CFrame
task.wait(3)
if doorState.Value == "open" then
killPlayersInArea()
npc.HumanoidRootPart.CFrame = startpoint.CFrame
else
npc.HumanoidRootPart.CFrame = startpoint.CFrame
print("Door Closed")
end
task.wait(5)
end
end
npcBehavior()