-- Server Script inside TriggerPart and part2
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local NPC = ServerStorage:FindFirstChild("npc")
local StartShakeEvent = ReplicatedStorage:WaitForChild("StartShakeEvent")
local StopShakeEvent = ReplicatedStorage:WaitForChild("StopShakeEvent")
local UnlockEvent = ReplicatedStorage:WaitForChild("UnlockEvent") -- New RemoteEvent

-- Function to handle "TriggerPart" touch
local function handleTriggerPartTouch(hit)
	local character = hit.Parent
	local player = Players:GetPlayerFromCharacter(character)
	if player then
		-- Prevent multiple triggers
		hit.Parent:FindFirstChild("TriggerPart"):Destroy()

		-- Summon NPC
		local npcClone = NPC:Clone()
		npcClone.Parent = workspace
		-- Position NPC at spawn location
		local npcSpawn = workspace:FindFirstChild("NPCSpawnLocation")
		if npcSpawn then
			npcClone:MoveTo(npcSpawn.Position)
		else
			warn("NPCSpawnLocation not found in Workspace.")
		end

		-- Notify client to start screen shake
		StartShakeEvent:FireClient(player)

		-- NPC chase logic
		local humanoid = npcClone:WaitForChild("Humanoid")
		local humanoidRootPart = npcClone:WaitForChild("HumanoidRootPart")

		local function chasePlayer()
			if npcClone and npcClone.Parent and character and character.Parent then
				humanoid:MoveTo(character.HumanoidRootPart.Position)
			end
		end

		local chaseConnection
		chaseConnection = RunService.Stepped:Connect(function()
			chasePlayer()
		end)

		-- Detect when NPC touches the player
		local touchedConnection
		touchedConnection = npcClone.HumanoidRootPart.Touched:Connect(function(otherPart)
			if otherPart:IsDescendantOf(character) then
				print("GAME OVER")
				-- Clean up
				if npcClone then npcClone:Destroy() end
				StopShakeEvent:FireClient(player)
				chaseConnection:Disconnect()
				touchedConnection:Disconnect()
			end
		end)

		-- Monitor if player is hiding
		local hidingCheckConnection
		hidingCheckConnection = RunService.Heartbeat:Connect(function()
			if character and character.Parent then
				local isHiding = false
				for _, part in ipairs(character:GetDescendants()) do
					if part:IsA("BasePart") then
						local touchingParts = part:GetTouchingParts()
						for _, touchedPart in ipairs(touchingParts) do
							if touchedPart.Name == "hide" then
								isHiding = true
								break
							end
						end
					end
					if isHiding then break end
				end
				if isHiding then
					-- Player is hiding
					if npcClone then npcClone:Destroy() end
					StopShakeEvent:FireClient(player)
					-- Clean up
					chaseConnection:Disconnect()
					touchedConnection:Disconnect()
					hidingCheckConnection:Disconnect()
				end
			else
				-- Player character removed
				hidingCheckConnection:Disconnect()
			end
		end)
	end
end

-- Function to handle "part2" touch
local function handlePart2Touch(hit)
	local character = hit.Parent
	local player = Players:GetPlayerFromCharacter(character)
	if player then
		-- Prevent multiple triggers
		local part2 = workspace:FindFirstChild("part2")
		if part2 then
			part2:Destroy()
		else
			warn("'part2' not found in Workspace.")
		end

		-- Fire the UnlockEvent to the client
		UnlockEvent:FireClient(player)
	end
end

-- Connect the Touched event for TriggerPart and part2
for _, partName in ipairs({"TriggerPart", "part2"}) do
	local part = script.Parent:FindFirstChild(partName)
	if part and part:IsA("BasePart") then
		if partName == "TriggerPart" then
			part.Touched:Connect(handleTriggerPartTouch)
		elseif partName == "part2" then
			part.Touched:Connect(handlePart2Touch)
		end
	else
		warn(partName .. " not found or is not a BasePart.")
	end
end (it says the following parts are not found even when they are found)