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

How do a make a door open when 2 or more certain npcs die?

Asked by 4 years ago

So i have a code that opens a door when 1 npc dies but i need it to only open when 2 or more certain npcs die. So for example, when 1 npc dies the door stays closed, but when the other one dies the door opens.

function door()
    local open = script.Parent.Health
    local doorr = game.Workspace.part3
    if open <1 then
        doorr.Transparency = 0.5
        doorr.CanCollide = false
    end
end
script.Parent.Died:Connect(door)

I put this in the npc's humanoid

0
Just make a global variable that increases when an npc is killed, then make sure the value of the variable is 2 or more if you want. To make a global variable: _G.VariableName = 0 mixgingengerina10 223 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Try this:

local npc1 = workspace.NPC1 -- Change NPC1 to the name of your npc
local npc2 = workspace.NPC2 -- Change NPC2 to the name of your npc
local door = workspace.part3

local died = 0

npc1.Humanoid.Died:Connect(function()
    died = died + 1
end)

npc2.Humanoid.Died:Connect(function()
    died = died + 1
end)

coroutine.wrap(function()
    while wait() do
        if died == 2 then
            door.Transparency = 0.5
            door.CanCollide = false
            break
        end
    end
end)()

Whenever an NPC dies, the died value gets increased. Then the while loop inside the coroutine constantly checks if the died value is equal to 2. If it is, the door opens and the while loop breaks. Make the script a regular/server script inside Workspace or ServerScriptService.

0
ur the best scripter ever thx so much coolmanHDMI 72 — 4y
0
it didnt work idk what happend coolmanHDMI 72 — 4y
0
NVM ITS SOLVED coolmanHDMI 72 — 4y
Ad

Answer this question