我想制作一个在 Roblox 游戏中可以改变文本的 GUI,但它不起作用

game.StarterGui.ScreenGui.TextButton.MouseButton1Click:Connect(function()
        game.StarterGui.ScreenGui.TextButton.Text = ("已点击")
        wait(1)
        game.StarterGui.ScreenGui.TextButton.Text = ("点我")
end)

我在使用 Roblox Studio 编程上很新手,所以我可能犯了一个简单的错误。

点赞
用户12666680
用户12666680

你的问题在于你使用的脚本是在 Workspace 下而不是 StarterGui 下。

当你测试游戏时,你会发现 StarterGui 下的所有项目都被移动到 “Players” 中的 Player objects 下。你需要将此脚本移动到 ScreenGui 内部并参考以下方式:

-- 父对象
local screenUI = script.Parent

screenUI.TextButton.MouseButton1Click:Connect(function()
    screenUI.TextButton.Text = ("Clicked")
    wait(1)
    screenUI.TextButton.Text = ("CLICK ME.")
end)
2020-01-07 08:14:45
用户13825230
用户13825230

请确保将此代码放置在 TextLabelStarterPlayer 或者其他 GUI 组件中的 LocalScript 中。服务器端脚本不能操作 UI。您的代码应该像这样:

local button = script.Parent --根据脚本位置而异

button.MouseButton1Click:Connect(function()
    button.Text = "Clicked" --按钮显示 "Clicked"
    wait(1) --等待一秒钟
    button.Text = "CLICK ME." --按钮显示 "CLICK ME."
end)
2020-06-27 17:31:02