【问题标题】:Why doesn't this change gui code work for Roblox Studio?为什么此更改 gui 代码不适用于 Roblox Studio?
【发布时间】:2020-06-20 05:12:00
【问题描述】:

我正在尝试创建一个每 5 秒更改一次的 TextLabel。我有这个代码,但它不起作用。

local player = game.Players:GetPlayerFromCharacter(part.Parent) —this is our gateway to getting the PlayerGui object.
  local PlayerUI = player:WaitForChild(“PlayerGui”)
  local txtLabel = PlayerUI[“Welcome_Text”].TextLabel
while x < 1 do
  wait(5)
  txtLabel.Text = “Welcome to The Shadow Realm!”
  wait(5)
  txtLabel.Text = “Warning: This game contains scenes that may be too scary for some roblox players”
end 

我收到一条错误消息。

ServerScriptService.Script:2:尝试索引全局“部分”(零值)

我不知道把我的 gui 放在哪里。

【问题讨论】:

  • 你没有在代码中的任何地方声明变量'part',也就是说,它是nil——因此出现attempt to index global 'part' (a nil value)的错误。您还将遇到与变量 'x' 相同的问题,因为它未声明。另外,您的上述代码将放在哪里,它会在ScriptLocalScript 中吗?
  • 抱歉,我不小心剪掉了x=0 位。代码将放置在脚本中,而不是本地脚本中。

标签: lua roblox


【解决方案1】:

如果我正确理解您要做什么,您应该能够创建一个ScreenGui 并将其放在StartGui 中,这样每个玩家在加入游戏时都会将其复制到他的PlayerGui .您可以在该 GUI 内放置一个 LocalScript 来控制屏幕上的文本。

我看到你的 LocalScript 看起来像这样:

-- Customize names on your own; these are just generic placeholders.
-- The structure of the GUI would look like this:
--
-- ScreenGui
--    LocalScript
--    Frame
--        TextLabel

local WELCOME = "Welcome to the Shadow Realm!"
local WARNING = "Warning! This game contains scenes that may be too scary for some players"

local runService = game:GetService("RunService")

local gui = script.Parent
local frame = gui.Frame
local label = frame.TextLabel

local function update_text()
    label.Text = WELCOME
    wait(5)
    label.Text = WARNING
    return
end
runService.RenderStepped:Connect(update_text)

在客户端 GUI 中将其设为 LocalScript 会将开销转移到客户端,并且无需使用方法 Players::GetPlayerFromCharacter

【讨论】:

    猜你喜欢
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2021-04-16
    • 2019-08-28
    相关资源
    最近更新 更多