--NPC Script
--This script opens the door if an NPC touches it.
--Unless you know what you're doing, do not modify this script.

local opening = false --This creates a bool to check if the door is opening.
script.Parent.Touched:Connect(function(hit) --This detects a touch on the door.
	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid") --This identifies the humanoid.
	if humanoid ~= nil and script.Parent.CanCollide == true then --This checks if a character touched the door and the door is closed.
		local humanoidwalkspeed --This creates a value to save the walk speed of the humanoid.
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) --This finds a player in the server.
		if player == nil and opening == false then --This ensures that there is no connected player to the character and that the door isn't opening yet.
			opening = true --This ensures that the script does not repeat the currently running code.
			humanoidwalkspeed = humanoid.WalkSpeed --This saves the current walk speed of the humanoid.
			wait(0.001) --This gives the script time to completely save the humanoid's current walk speed.
			humanoid.WalkSpeed = 0 --This stops the humanoid from walking.
			script.Parent.Unlock:Play() --This plays the "Door Knob" sound.
			wait(1.1) --There is a delay for opening the door.
			script.Parent.Open:Play() --This plays the door open sound.
			script.Parent.Transparency = 0.25 --This causes the door to be slightly transparent.
			script.Parent.CanCollide = false --This causes the door to stop colliding.
			wait(1) --The NPC will delay before walking through the door.
			humanoid.WalkSpeed = humanoidwalkspeed --This resets the humanoid walk speed to the original.
			wait(1) --This gives the NPC time to stop touching the door.
			opening = false --This allows the script to run the code again as long as the door is closed.
		end
	end
end)
