How to make a human only door

Hello developers.

I am trying to make a human only door that will only allow players to access the door but I am really bad at scripting so what would I need to do.

The door is supposed to keep zombies out NCP

At the moment I have inserted “part”

1 Like
local door = script.Parent

door.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local character = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			door.CanCollide = false
			door.Transparency = 1
			task.wait(5)
			door.CanCollide = true
			door.Transparency = 0
		elseif not player then
			hit.Parent:BreakJoints()
		end
	end
end)

This will allow players inside and if an NPC attempts to get inside it will kill them.

2 Likes
Door.Touched:Connect(function(hit)
   local char = hit.Parent
   local hum = char:FindFirstChild("Humanoid")
   if hum:IsA("Humanoid") and game.Players:GetPlayerFromCharacter(target.Parent) then
       --code

end)

Do i just need to name my part “Door”?

Nah, can be named anything (just make sure it’s a part) and the script needs to be placed inside it.

Hey could you double check your script it seems not to kill the NCP when they touch it.

and a slight delay when you touch it

the zombies named “0”

Can you send a screenshot of the NPC’s children/descendants?

local door = script.Parent
local debounce = false

door.Touched:Connect(function(hit)
	if debounce then
		return
	end
	if hit.Parent:FindFirstChild("Humanoid") then
		local character = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			debounce = true
			door.CanCollide = false
			door.Transparency = 1
			task.wait(5)
			debounce = false
			door.CanCollide = true
			door.Transparency = 0
		end
		if hit:FindFirstAncestorWhichIsA("Model").Name == "0" then
			hit:FindFirstAncestorWhichIsA("Model"):FindFirstDescendant("Humanoid").Health = 0
		end
	end
end)

Added cooldown, and attempted to reference the NPC correctly.

I would recommend you look into CollisionGroups. You could assign players and zombies a separate one.

local door = script.Parent
local debounce = false

door.Touched:Connect(function(hit)
	if debounce then
		return
	end
	if hit.Parent:FindFirstChild("Humanoid") then
		local character = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			debounce = true
			door.CanCollide = false
			door.Transparency = 1
			task.wait(5)
			debounce = false
			door.CanCollide = true
			door.Transparency = 0
		end
		--define zombies here and kill them
	end
end)

Here, since you can see the children/descendants etc. of the zombie it shouldn’t be too hard to implement the check yourself. I have no reference to go off.

script.Parent.Touched:Connect(function(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then
        -- Do stuff.
    else
    return false
end)
local door = script.Parent
local debounce = false

door.Touched:Connect(function(hit)
	if debounce then
		return
	end
	if hit.Parent:FindFirstChild("Humanoid") then
		local character = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			debounce = true
			door.CanCollide = false
			door.Transparency = 1
			task.wait(5)
			debounce = false
			door.CanCollide = true
			door.Transparency = 0
		else
			door.CanCollide = true
			door.Transparency = 0
		end
	end
end)

Does this one 100% work? Sorry I’m bad at scripting