如何创建一个 Roblox 游戏,玩家必须猜测一个随机生成的 PIN 码?

所以,我已经在过去的一周里一直在这方面工作。我尝试了所有我知道的东西,但是什么也没做到……我的代码第一次、第二次、第三次……第四次都不行……最后,我让沮丧控制了我,我把整个脚本都删掉了。幸运的是,没有删掉零件和模型,否则我会真的很惨...

我需要创建一个游戏,在游戏中我必须创建一个键盘,最初我认为 GUI 可以工作……但不可以,它需要是 SurfaceGUI,而我不知道如何处理……无论如何,我需要使用 SurfaceGUI 创建一个键盘,并在单独的屏幕上显示,就像典型的键盘一样...

玩家首先必须输入一个“初始”数字,这意味着为了输入随机生成的数字,他首先需要输入静态 PIN 码进行“登录”,之后,他就会试着猜这个数字……

我已经尝试了我能想到的一切,但是都不行……主要是因为我缺乏 LUA 方面的经验,我在 Python 方面更加高级,而在 Java 方面几乎一无所知……如果有人能帮助我如何做到这一点,我会非常感激。

点赞
用户13072308
用户13072308

首先,下载这个文件,然后将它放入StarterGui里的ScreenGui。接下来,在PIN框架中使用下面的LocalScript

-- 脚本设置

local len = 4 -- 根据需要替换...
local regen = false -- 确定在失败的情况下是否重新生成PIN码
local regmes = "Enter PIN..." -- PIN面板的起始消息
local badmes = "Wrong PIN!" -- 在PIN错时显示的消息
local success = "Correct PIN!" -- 在PIN正确时显示的消息

-- 脚本内容

local pin = script.Parent
local top = pin.Top
local txt = top.Numbers
local nums = top.NumKeys
local pin
local stpin
local nms
txt.Text = regmes
local see = game:GetStorage("ReplicatedStorage").PINActivate
local function activate()
    if pin.Visible then
        pin.Visible = false
        for _, btn in pairs(nums:GetChildren()) do
            btn.Active = false
        end
        return
    else
        pin.Visible = true
        for _, btn in pairs(nums:GetChildren()) do
            btn.Active = true
        end
        return
    end
end
local function rand()
    math.randomseed(os.time) -- 用这种方式获得更好的随机数
    return tostring(math.floor(math.random(0,9.9)))
end
local function gen()
    nms = {rand()}
    for i=2, len, 1 do
        nms[#nms+1]=rand()
    end
    stpin = nms[1]
    for i=2, #nms, 1 do
        stpin = stpin..nms[i]
    end
    pin = tonumber(stpin) -- 将数字字符串转换为整数
end
gen()
local function activate(str)
    if tonumber(str) ~= pin then
        txt.Text = badmes
        wait(2)
        txt.Text = regmes
        if regen then
            gen()
            wait(0.1)
        end
        return
    else
        txt.Text = success
        wait(2)
        activate()
        -- 在此处插入代码...
    end
end
for _, btn in pairs(nums:GetChildren()) do
    btn.Activated:Connect(function()
        if txt.Text == "Wrong PIN!" then return end
        txt.Text = txt.Text..btn.Text
        if string.len(txt.Text) >= len then
            activate(txt.Text)
        end
        wait(0.1)
    end)
end
see.OnClientEvent:Connect(activate)

然后在一个脚本中加入以下内容:

local Players = game:GetService("Players")
local see = game:GetService("ReplicatedStorage").PINActivate
local plr
-- 将Event替换为像Part.Touched这样的内容
Event:Connect(function(part)
    if part.Parent.Head then
        plr = Players:GetPlayerFromCharacter(part.Parent)
        see:FireClient(plr)
    end
end)

这将会给该玩家显示一个ScreenGui,让他们输入PIN码,也可以关闭它。您可以根据需要进行修改;祝您今天愉快! :D

2020-05-04 21:13:39
用户20422121
用户20422121

以下是中文翻译,保留原始的 markdown 格式:

有一种更简单的方法,试试这个:

首先,在 StarterGui 中创建 GUI,然后创建一个文本框并放置,之后在里面创建一个本地脚本并键入以下内容:


local Password = math.random(1000, 9999)

print(Password)
game.ReplicatedStorage.Password.Value = Password

script.Parent.FocusLost:Connect(function(enter)
    if enter then
        if script.Parent.Text == tostring(Password) then
            print("Correct!")
            script.Parent.BorderColor3 = Color3.new(0, 255, 0)
            Password = math.random(1000, 9999)
            game.ReplicatedStorage.Correct1:FireServer()
            print(Password)
            game.ReplicatedStorage.Password.Value = Password
        else
            print("wrong!")
            print(script.Parent.Text)
            script.Parent.BorderColor3 = Color3.new(255, 0, 0)
        end
    end
end)

这就是在文本框中的全部内容。或者如果你想要一个随机用户名脚本,可以创建一个文本标签,然后在文本标签中创建本地脚本并键入以下内容:


local UserText = script.Parent
local Username = math.random(1,10)

while true do
    if Username == 1 then
        UserText.Text = "John"
    elseif Username == 2 then
        UserText.Text = "Thomas"
    elseif Username == 3 then
        UserText.Text = "Raymond"
    elseif Username == 4 then
        UserText.Text = "Ray"
    elseif Username == 5 then
        UserText.Text = "Tom"
    elseif Username == 6 then
        UserText.Text = "Kai"
    elseif Username == 7 then
        UserText.Text = "Lloyd"
    elseif Username == 8 then
        UserText.Text = "Jay"
    elseif Username == 9 then
        UserText.Text = "User"
    else
        UserText.Text = "Guest"
    end
    wait()
end

所有这些 if 语句都是在检查选择了哪个用户名。最近我制作了一个类似这样的 Roblox 游戏,因此我只是从游戏中提取了所有脚本。

如果你想要查看我的游戏,点击这里

2022-11-05 14:18:03