Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local GetSecretCode = ReplicatedStorage:WaitForChild("GetSecretCode")
- local CheckSecretCode = ReplicatedStorage:WaitForChild("CheckSecretCode")
- -- =====================================================
- -- НАСТРОЙКИ
- -- =====================================================
- local CODE_LENGTH = 8
- -- Английские буквы + цифры
- local CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
- -- =====================================================
- -- ТАБЛИЦА КОДОВ ИГРОКОВ
- -- =====================================================
- local playerCodes = {}
- -- =====================================================
- -- ГЕНЕРАЦИЯ СЛУЧАЙНОГО КОДА
- -- =====================================================
- local function generateCode(length)
- local code = ""
- for i = 1, length do
- local randomIndex = math.random(1, #CHARACTERS)
- local randomCharacter =
- string.sub(CHARACTERS, randomIndex, randomIndex)
- code = code .. randomCharacter
- end
- return code
- end
- -- =====================================================
- -- КОГДА ИГРОК ЗАХОДИТ
- -- =====================================================
- Players.PlayerAdded:Connect(function(player)
- local newCode = generateCode(CODE_LENGTH)
- playerCodes[player.UserId] = newCode
- print(
- "Игрок:",
- player.Name,
- "| Его код:",
- newCode
- )
- end)
- -- =====================================================
- -- КОГДА ИГРОК ВЫХОДИТ
- -- =====================================================
- Players.PlayerRemoving:Connect(function(player)
- playerCodes[player.UserId] = nil
- end)
- -- =====================================================
- -- ПОЛУЧЕНИЕ КОДА ИГРОКОМ
- -- =====================================================
- GetSecretCode.OnServerInvoke = function(player)
- local code = playerCodes[player.UserId]
- return code
- end
- -- =====================================================
- -- ПРОВЕРКА ВВЕДЁННОГО КОДА
- -- =====================================================
- CheckSecretCode.OnServerEvent:Connect(function(player, enteredCode)
- -- Получаем настоящий код именно этого игрока
- local correctCode = playerCodes[player.UserId]
- if not correctCode then
- return
- end
- -- Проверяем, что пришла строка
- if typeof(enteredCode) ~= "string" then
- return
- end
- -- Убираем пробелы в начале и конце
- enteredCode = string.gsub(
- enteredCode,
- "^%s+",
- ""
- )
- enteredCode = string.gsub(
- enteredCode,
- "%s+$",
- ""
- )
- -- Сравнение
- local isCorrect = enteredCode == correctCode
- -- Отправляем результат только этому игроку
- CheckSecretCode:FireClient(
- player,
- isCorrect
- )
- end)
Advertisement
Add Comment
Please, Sign In to add comment