Hasli4

Untitled

Aug 31st, 2026
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.95 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3.  
  4. local TextService = game:GetService("TextService")
  5.  
  6.  
  7. --------------------------------------------------
  8. -- RemoteEvents
  9. --------------------------------------------------
  10.  
  11. local submitPlayerName =
  12. ReplicatedStorage:WaitForChild("SubmitPlayerName")
  13.  
  14. local showDialogResponse =
  15. ReplicatedStorage:WaitForChild("ShowDialogResponse")
  16.  
  17.  
  18. --------------------------------------------------
  19. -- NPC
  20. --------------------------------------------------
  21.  
  22. local npc = workspace:WaitForChild("NPC")
  23.  
  24. local humanoid =
  25. npc:WaitForChild("Humanoid")
  26.  
  27. local rootPart =
  28. npc:WaitForChild("HumanoidRootPart")
  29.  
  30.  
  31. --------------------------------------------------
  32. -- NPC PATH
  33. --------------------------------------------------
  34.  
  35. local npcPath =
  36. workspace:WaitForChild("NPCPath")
  37.  
  38. local startPoint =
  39. npcPath:WaitForChild("Start")
  40.  
  41. local middlePoint =
  42. npcPath:WaitForChild("Middle")
  43.  
  44. local doorPoint =
  45. npcPath:WaitForChild("Door")
  46.  
  47. local cornerPoint =
  48. npcPath:WaitForChild("Corner")
  49.  
  50.  
  51. --------------------------------------------------
  52. -- WALK ANIMATION
  53. --------------------------------------------------
  54.  
  55. local walkAnimation =
  56. npc:WaitForChild("WalkAnimation")
  57.  
  58.  
  59. --------------------------------------------------
  60. -- Имена игроков
  61. --
  62. -- UserId -> имя персонажа
  63. --------------------------------------------------
  64.  
  65. local playerNames = {}
  66.  
  67.  
  68. --------------------------------------------------
  69. -- Состояние NPC
  70. --------------------------------------------------
  71.  
  72. local npcLeaving = false
  73. local npcGone = false
  74.  
  75.  
  76. --------------------------------------------------
  77. -- Настройки
  78. --------------------------------------------------
  79.  
  80. local RESPONSE_DURATION = 3
  81.  
  82. local WALK_SPEED = 8
  83.  
  84.  
  85. --------------------------------------------------
  86. -- Настройка Humanoid
  87. --------------------------------------------------
  88.  
  89. humanoid.WalkSpeed = WALK_SPEED
  90. humanoid.AutoRotate = true
  91. humanoid.AutoJumpEnabled = false
  92.  
  93.  
  94. --------------------------------------------------
  95. -- Animator
  96. --------------------------------------------------
  97.  
  98. local animator =
  99. humanoid:FindFirstChildOfClass("Animator")
  100.  
  101.  
  102. if not animator then
  103.  
  104. animator = Instance.new("Animator")
  105. animator.Parent = humanoid
  106.  
  107. end
  108.  
  109.  
  110. --------------------------------------------------
  111. -- Загружаем ходьбу
  112. --------------------------------------------------
  113.  
  114. local walkTrack
  115.  
  116. local animationSuccess, animationResult =
  117. pcall(function()
  118.  
  119. return animator:LoadAnimation(
  120. walkAnimation
  121. )
  122.  
  123. end)
  124.  
  125.  
  126. if animationSuccess then
  127.  
  128. walkTrack = animationResult
  129.  
  130. walkTrack.Looped = true
  131.  
  132. walkTrack.Priority =
  133. Enum.AnimationPriority.Movement
  134.  
  135. print("[NPC] Анимация ходьбы загружена")
  136.  
  137. else
  138.  
  139. warn(
  140. "[NPC] Не удалось загрузить WalkAnimation:",
  141. animationResult
  142. )
  143.  
  144. end
  145.  
  146.  
  147. --------------------------------------------------
  148. -- Функция запуска ходьбы
  149. --------------------------------------------------
  150.  
  151. local function startWalkingAnimation()
  152.  
  153. if not walkTrack then
  154. return
  155. end
  156.  
  157. if not walkTrack.IsPlaying then
  158.  
  159. walkTrack:Play(
  160. 0.2,
  161. 1,
  162. 1
  163. )
  164.  
  165. end
  166.  
  167. end
  168.  
  169.  
  170. --------------------------------------------------
  171. -- Функция остановки ходьбы
  172. --------------------------------------------------
  173.  
  174. local function stopWalkingAnimation()
  175.  
  176. if not walkTrack then
  177. return
  178. end
  179.  
  180. if walkTrack.IsPlaying then
  181.  
  182. walkTrack:Stop(0.2)
  183.  
  184. end
  185.  
  186. end
  187.  
  188.  
  189. --------------------------------------------------
  190. -- Перемещение к точке
  191. --------------------------------------------------
  192.  
  193. local function moveToPoint(point)
  194.  
  195. if npcGone then
  196. return false
  197. end
  198.  
  199.  
  200. print(
  201. "[NPC] Иду к точке:",
  202. point.Name
  203. )
  204.  
  205.  
  206. --------------------------------------------------
  207. -- Сохраняем текущую высоту NPC
  208. --------------------------------------------------
  209.  
  210. local targetPosition = Vector3.new(
  211. point.Position.X,
  212. rootPart.Position.Y,
  213. point.Position.Z
  214. )
  215.  
  216.  
  217. --------------------------------------------------
  218. -- Запускаем анимацию
  219. --------------------------------------------------
  220.  
  221. startWalkingAnimation()
  222.  
  223.  
  224. --------------------------------------------------
  225. -- Функция повторного MoveTo
  226. --
  227. -- Это предотвращает 8-секундный таймаут.
  228. --------------------------------------------------
  229.  
  230. local reached = false
  231. local finished = false
  232.  
  233.  
  234. local connection
  235.  
  236. connection =
  237. humanoid.MoveToFinished:Connect(function(success)
  238.  
  239. reached = success
  240. finished = true
  241.  
  242. end)
  243.  
  244.  
  245. humanoid:MoveTo(targetPosition)
  246.  
  247.  
  248. --------------------------------------------------
  249. -- Обновляем MoveTo каждые несколько секунд
  250. --------------------------------------------------
  251.  
  252. local startTime = os.clock()
  253.  
  254. while not finished do
  255.  
  256. if npcGone then
  257. break
  258. end
  259.  
  260.  
  261. -- Если NPC почему-то перестал идти,
  262. -- снова задаём точку.
  263.  
  264. if os.clock() - startTime >= 5 then
  265.  
  266. humanoid:MoveTo(targetPosition)
  267.  
  268. startTime = os.clock()
  269.  
  270. end
  271.  
  272.  
  273. task.wait(0.1)
  274.  
  275. end
  276.  
  277.  
  278. if connection then
  279. connection:Disconnect()
  280. end
  281.  
  282.  
  283. --------------------------------------------------
  284. -- Если достигли точки
  285. --------------------------------------------------
  286.  
  287. if reached then
  288.  
  289. print(
  290. "[NPC] Достиг точки:",
  291. point.Name
  292. )
  293.  
  294. else
  295.  
  296. warn(
  297. "[NPC] Не смог достичь точки:",
  298. point.Name
  299. )
  300.  
  301. end
  302.  
  303.  
  304. return reached
  305.  
  306. end
  307.  
  308.  
  309. --------------------------------------------------
  310. -- Уход NPC
  311. --------------------------------------------------
  312.  
  313. local function makeNpcLeave()
  314.  
  315. if npcLeaving then
  316. return
  317. end
  318.  
  319. if npcGone then
  320. return
  321. end
  322.  
  323.  
  324. npcLeaving = true
  325.  
  326.  
  327. print("========================================")
  328. print("[NPC] НАЧИНАЮ УХОД")
  329. print("========================================")
  330.  
  331.  
  332. --------------------------------------------------
  333. -- Останавливаем возможное движение
  334. --------------------------------------------------
  335.  
  336. humanoid:MoveTo(
  337. Vector3.new(
  338. rootPart.Position.X,
  339. rootPart.Position.Y,
  340. rootPart.Position.Z
  341. )
  342. )
  343.  
  344.  
  345. --------------------------------------------------
  346. -- Сначала идём к Start
  347. --------------------------------------------------
  348.  
  349. local startReached =
  350. moveToPoint(startPoint)
  351.  
  352. if not startReached then
  353.  
  354. warn(
  355. "[NPC] Не удалось дойти до Start"
  356. )
  357.  
  358. end
  359.  
  360.  
  361. --------------------------------------------------
  362. -- Затем в середину комнаты
  363. --------------------------------------------------
  364.  
  365. local middleReached =
  366. moveToPoint(middlePoint)
  367.  
  368. if not middleReached then
  369.  
  370. warn(
  371. "[NPC] Не удалось дойти до Middle"
  372. )
  373.  
  374. end
  375.  
  376.  
  377. --------------------------------------------------
  378. -- Затем за дверь
  379. --------------------------------------------------
  380.  
  381. local doorReached =
  382. moveToPoint(doorPoint)
  383.  
  384. if not doorReached then
  385.  
  386. warn(
  387. "[NPC] Не удалось дойти до Door"
  388. )
  389.  
  390. end
  391.  
  392.  
  393. --------------------------------------------------
  394. -- Затем за угол
  395. --------------------------------------------------
  396.  
  397. local cornerReached =
  398. moveToPoint(cornerPoint)
  399.  
  400. if not cornerReached then
  401.  
  402. warn(
  403. "[NPC] Не удалось дойти до Corner"
  404. )
  405.  
  406. end
  407.  
  408.  
  409. --------------------------------------------------
  410. -- Останавливаем анимацию
  411. --------------------------------------------------
  412.  
  413. stopWalkingAnimation()
  414.  
  415.  
  416. --------------------------------------------------
  417. -- NPC исчезает
  418. --------------------------------------------------
  419.  
  420. if npc.Parent then
  421.  
  422. print(
  423. "[NPC] За углом. УДАЛЯЮ NPC."
  424. )
  425.  
  426. npcGone = true
  427.  
  428. npc:Destroy()
  429.  
  430. end
  431.  
  432. end
  433.  
  434.  
  435. --------------------------------------------------
  436. -- Получение имени игрока
  437. --------------------------------------------------
  438.  
  439. submitPlayerName.OnServerEvent:Connect(
  440. function(player, rawName)
  441.  
  442. print("========================================")
  443. print("[DIALOG SERVER] Получено имя:")
  444. print("[DIALOG SERVER] Игрок:", player.Name)
  445. print("[DIALOG SERVER] UserId:", player.UserId)
  446. print("[DIALOG SERVER] Имя:", rawName)
  447. print("========================================")
  448.  
  449.  
  450. --------------------------------------------------
  451. -- Если NPC уже ушёл
  452. --------------------------------------------------
  453.  
  454. if npcLeaving or npcGone then
  455.  
  456. warn(
  457. "[DIALOG SERVER] NPC уже уходит/ушёл"
  458. )
  459.  
  460. return
  461.  
  462. end
  463.  
  464.  
  465. --------------------------------------------------
  466. -- Проверяем тип
  467. --------------------------------------------------
  468.  
  469. if typeof(rawName) ~= "string" then
  470.  
  471. warn(
  472. "[DIALOG SERVER] Имя не строка"
  473. )
  474.  
  475. return
  476.  
  477. end
  478.  
  479.  
  480. --------------------------------------------------
  481. -- Убираем пробелы
  482. --------------------------------------------------
  483.  
  484. rawName =
  485. rawName:match("^%s*(.-)%s*$")
  486.  
  487.  
  488. --------------------------------------------------
  489. -- Проверяем пустое имя
  490. --------------------------------------------------
  491.  
  492. if rawName == "" then
  493.  
  494. warn(
  495. "[DIALOG SERVER] Игрок отправил пустое имя"
  496. )
  497.  
  498. return
  499.  
  500. end
  501.  
  502.  
  503. --------------------------------------------------
  504. -- Ограничение длины
  505. --------------------------------------------------
  506.  
  507. local length =
  508. utf8.len(rawName)
  509.  
  510.  
  511. if not length then
  512.  
  513. warn(
  514. "[DIALOG SERVER] Некорректный UTF-8 текст"
  515. )
  516.  
  517. return
  518.  
  519. end
  520.  
  521.  
  522. if length > 20 then
  523.  
  524. local cutPosition =
  525. utf8.offset(rawName, 21)
  526.  
  527. if cutPosition then
  528.  
  529. rawName =
  530. string.sub(
  531. rawName,
  532. 1,
  533. cutPosition - 1
  534. )
  535.  
  536. end
  537.  
  538. end
  539.  
  540.  
  541. --------------------------------------------------
  542. -- Фильтрация
  543. --------------------------------------------------
  544.  
  545. print(
  546. "[DIALOG SERVER] Фильтруем имя..."
  547. )
  548.  
  549.  
  550. local success, filterResult =
  551. pcall(function()
  552.  
  553. return TextService:FilterStringAsync(
  554. rawName,
  555. player.UserId
  556. )
  557.  
  558. end)
  559.  
  560.  
  561. if not success then
  562.  
  563. warn(
  564. "[DIALOG SERVER] Ошибка FilterStringAsync:",
  565. filterResult
  566. )
  567.  
  568. return
  569.  
  570. end
  571.  
  572.  
  573. --------------------------------------------------
  574. -- Получаем фильтрованный текст
  575. --------------------------------------------------
  576.  
  577. local successFiltered, filteredName =
  578. pcall(function()
  579.  
  580. return filterResult:GetNonChatStringForUserAsync(
  581. player.UserId
  582. )
  583.  
  584. end)
  585.  
  586.  
  587. if not successFiltered then
  588.  
  589. warn(
  590. "[DIALOG SERVER] Ошибка фильтрации:",
  591. filteredName
  592. )
  593.  
  594. return
  595.  
  596. end
  597.  
  598.  
  599. --------------------------------------------------
  600. -- Проверяем результат
  601. --------------------------------------------------
  602.  
  603. if typeof(filteredName) ~= "string" then
  604.  
  605. warn(
  606. "[DIALOG SERVER] filteredName не строка"
  607. )
  608.  
  609. return
  610.  
  611. end
  612.  
  613.  
  614. if filteredName == "" then
  615.  
  616. warn(
  617. "[DIALOG SERVER] После фильтрации имя пустое"
  618. )
  619.  
  620. return
  621.  
  622. end
  623.  
  624.  
  625. --------------------------------------------------
  626. -- Сохраняем имя игрока
  627. --------------------------------------------------
  628.  
  629. playerNames[player.UserId] =
  630. filteredName
  631.  
  632.  
  633. print(
  634. "[DIALOG SERVER] Имя сохранено:",
  635. playerNames[player.UserId]
  636. )
  637.  
  638.  
  639. --------------------------------------------------
  640. -- Формируем ответ
  641. --------------------------------------------------
  642.  
  643. local response =
  644. "Понятно, " .. filteredName .. "."
  645.  
  646.  
  647. print(
  648. "[DIALOG SERVER] Отправляем:",
  649. response
  650. )
  651.  
  652.  
  653. --------------------------------------------------
  654. -- Показываем ответ игроку
  655. --------------------------------------------------
  656.  
  657. showDialogResponse:FireClient(
  658. player,
  659. response
  660. )
  661.  
  662.  
  663. print(
  664. "[DIALOG SERVER] Ответ отправлен"
  665. )
  666.  
  667.  
  668. --------------------------------------------------
  669. -- Через 3 секунды NPC начинает уходить
  670. --------------------------------------------------
  671.  
  672. task.delay(
  673. RESPONSE_DURATION,
  674. function()
  675.  
  676. makeNpcLeave()
  677.  
  678. end
  679. )
  680.  
  681. end
  682. )
  683.  
  684.  
  685. --------------------------------------------------
  686. -- Игрок вышел
  687. --------------------------------------------------
  688.  
  689. Players.PlayerRemoving:Connect(
  690. function(player)
  691.  
  692. playerNames[player.UserId] = nil
  693.  
  694. end
  695. )
Advertisement
Add Comment
Please, Sign In to add comment