-- Server Script placed in ServerScriptService

-- Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- References to the required parts
local detector = workspace:FindFirstChild("detector")
local tp = workspace:FindFirstChild("tp")

-- Ensure the parts exist
if not detector then
	warn("Detector part not found in Workspace.")
	return
end

if not tp then
	warn("Teleport part (tp) not found in Workspace.")
	return
end

-- RemoteEvent to communicate with the client
local RemoteEvent = ReplicatedStorage:FindFirstChild("ChatRemoteEvent")
if not RemoteEvent then
	RemoteEvent = Instance.new("RemoteEvent")
	RemoteEvent.Name = "ChatRemoteEvent"
	RemoteEvent.Parent = ReplicatedStorage
end

-- Ensure the StartNightEvent exists
local StartNightEvent = ReplicatedStorage:FindFirstChild("StartNightEvent")
if not StartNightEvent then
	StartNightEvent = Instance.new("RemoteEvent")
	StartNightEvent.Name = "StartNightEvent"
	StartNightEvent.Parent = ReplicatedStorage
end

-- Variable to prevent multiple triggers
local touchDebounce = false

-- Function to handle the touch event
local function onTouched(otherPart)
	if touchDebounce then return end
	touchDebounce = true

	local character = otherPart.Parent
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		local player = Players:GetPlayerFromCharacter(character)
		if player then
			-- Teleport the player's HumanoidRootPart to the "tp" part
			local hrp = character:FindFirstChild("HumanoidRootPart")
			if hrp then
				hrp.CFrame = tp.CFrame
			else
				warn("HumanoidRootPart not found for character.")
				touchDebounce = false
				return
			end

			-- Set the player's walk speed to 0
			humanoid.WalkSpeed = 0

			-- Fire the RemoteEvent to the player's client to send the chat messages
			RemoteEvent:FireClient(player, "Welp. this is it. My new job.")

			-- Wait for 3 seconds
			task.wait(3)

			-- Send the second message
			RemoteEvent:FireClient(player, "I mean. how hard can it be? I just only have to watch the place for only 4 nights anyway!")

			-- Wait another 3 seconds
			task.wait(3)

			-- Fire the StartNightEvent to the player
			StartNightEvent:FireClient(player)

			-- Reset walk speed
			humanoid.WalkSpeed = 16  -- Reset to default walk speed

			-- Allow the touch event to trigger again after a delay (if needed)
			task.wait(1)
		end
	end
	touchDebounce = false
end

-- Connect the touch event to the detector part
detector.Touched:Connect(onTouched)
-- LocalScript placed in StarterPlayerScripts or inside the ScreenGui

-- Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

-- Wait for RemoteEvents
local ChatRemoteEvent = ReplicatedStorage:WaitForChild("ChatRemoteEvent")
local StartNightEvent = ReplicatedStorage:WaitForChild("StartNightEvent")

-- GUI references
local screenGui = playerGui:WaitForChild("ScreenGui")
screenGui.Enabled = false

local textLabel = screenGui:WaitForChild("TextLabel")

-- Handle chat messages
ChatRemoteEvent.OnClientEvent:Connect(function(message)
	-- Display the chat message to the player (you can customize how this is done)
	game.StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = message;
		Color = Color3.new(1, 1, 1); -- White color
		Font = Enum.Font.SourceSansBold;
		FontSize = Enum.FontSize.Size24;
	})
end)

-- Handle StartNightEvent
StartNightEvent.OnClientEvent:Connect(function()
	-- Start the night progression
	screenGui.Enabled = true
	textLabel.Text = "Night 1"

	-- Define the total duration in seconds (8 minutes 55 seconds)
	local totalDuration = 8 * 60 + 55  -- 535 seconds

	-- Number of nights
	local numNights = 5

	-- Interval between nights
	local interval = totalDuration / (numNights - 1)  -- Approx. 133.75 seconds per night

	local currentNight = 1

	-- Function to update the night
	local function updateNight()
		currentNight = currentNight + 1
		if currentNight <= numNights then
			textLabel.Text = "Night " .. currentNight
		end
	end

	-- Schedule night updates
	for i = 1, numNights - 1 do
		task.delay(interval * i, updateNight)
	end

	-- Optionally, you can add code to handle what happens after Night 5
end)
a textlabel named: "timer" would input 12AM. every 89 seconds. it would turn 1AM until 6AM. like FNaF. if it's 6AM. reset and it should turn 12AM. it should input 12AM aswell. reset