Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

1 comment

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

1 answer

✓ Accepted answer

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.

3 comments

0

ur the best scripter ever thx so much

0

it didnt work idk what happend

0

NVM ITS SOLVED

This discussion is preserved as it was. New answers, comments, and votes are closed.