Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local TextService = game:GetService("TextService")
- --------------------------------------------------
- -- RemoteEvents
- --------------------------------------------------
- local openNameInput = ReplicatedStorage:WaitForChild("OpenNameInput")
- local submitPlayerName = ReplicatedStorage:WaitForChild("SubmitPlayerName")
- local showDialogResponse = ReplicatedStorage:WaitForChild("ShowDialogResponse")
- --------------------------------------------------
- -- Dialog
- --------------------------------------------------
- local dialog = workspace.NPC.Head.Dialog
- local nameChoice = dialog:WaitForChild("NameChoice")
- --------------------------------------------------
- -- Имена игроков
- --
- -- Ключ = UserId
- -- Значение = введённое имя
- --
- -- Эти данные находятся только на сервере.
- --------------------------------------------------
- local playerNames = {}
- --------------------------------------------------
- -- Игрок выбрал "Меня зовут..."
- --------------------------------------------------
- dialog.DialogChoiceSelected:Connect(function(player, choice)
- -- Нас интересует только NameChoice
- if choice ~= nameChoice then
- return
- end
- -- Открываем окно только этому игроку
- openNameInput:FireClient(player)
- end)
- --------------------------------------------------
- -- Игрок отправил имя
- --------------------------------------------------
- submitPlayerName.OnServerEvent:Connect(function(player, rawName)
- --------------------------------------------------
- -- Проверяем тип данных
- --------------------------------------------------
- if typeof(rawName) ~= "string" then
- return
- end
- --------------------------------------------------
- -- Убираем пробелы
- --------------------------------------------------
- rawName = rawName:match("^%s*(.-)%s*$")
- --------------------------------------------------
- -- Пустое имя
- --------------------------------------------------
- if rawName == "" then
- return
- end
- --------------------------------------------------
- -- Ограничиваем длину
- --------------------------------------------------
- local nameLength = utf8.len(rawName)
- if not nameLength then
- return
- end
- if nameLength > 20 then
- local position = utf8.offset(rawName, 21)
- if position then
- rawName = string.sub(rawName, 1, position - 1)
- end
- end
- --------------------------------------------------
- -- Фильтрация текста
- --------------------------------------------------
- local success, filterResult = pcall(function()
- return TextService:FilterStringAsync(
- rawName,
- player.UserId
- )
- end)
- if not success then
- warn(
- "Не удалось отфильтровать имя:",
- player.Name
- )
- return
- end
- --------------------------------------------------
- -- Получаем текст, который можно показать
- -- этому игроку
- --------------------------------------------------
- local successFiltered, filteredName = pcall(function()
- return filterResult:GetNonChatStringForUserAsync(
- player.UserId
- )
- end)
- if not successFiltered then
- warn(
- "Не удалось получить отфильтрованное имя:",
- player.Name
- )
- return
- end
- --------------------------------------------------
- -- Дополнительная проверка
- --------------------------------------------------
- if typeof(filteredName) ~= "string" then
- return
- end
- if filteredName == "" then
- return
- end
- --------------------------------------------------
- -- Сохраняем имя КОНКРЕТНОГО игрока
- --------------------------------------------------
- playerNames[player.UserId] = filteredName
- print(
- "Имя персонажа:",
- player.Name,
- "→",
- filteredName
- )
- --------------------------------------------------
- -- Формируем персональный ответ
- --------------------------------------------------
- local response =
- "Понятно, " .. filteredName .. "."
- --------------------------------------------------
- -- Отправляем ответ ТОЛЬКО этому игроку
- --------------------------------------------------
- showDialogResponse:FireClient(
- player,
- response
- )
- end)
- --------------------------------------------------
- -- Игрок вышел
- --------------------------------------------------
- Players.PlayerRemoving:Connect(function(player)
- playerNames[player.UserId] = nil
- end)
Advertisement
Add Comment
Please, Sign In to add comment