【问题标题】:Roblox Error: attempt to index local 'screengui' (a nil value)Roblox 错误:尝试索引本地“screengui”(零值)
【发布时间】:2018-08-19 10:05:02
【问题描述】:

Workspace.Part.Script:16:尝试索引本地“screengui”(零值)

wait(2) -- Testing to make sure assets loaded

script.Parent.Touched:connect(function(hit)
    if not hit or not hit.Parent then return end
    local human = hit.Parent:findFirstChild("Humanoid")
    if human and human:IsA("Humanoid") then
        local person = game.Players:GetPlayerFromCharacter(human.parent)
        if not person then return end
        person.Checklist.FirstEggCollected.Value = true
        local playgui = person:FindFirstChild('PlayerGui')
        print(playgui)
        wait(0.2)
        local screengui = playgui:FindFirstChild('ScreenGui')
        wait(0.2)
        print(screengui) -- This prints nil
        local collectnotice = screengui:FindFirstChild('CollectionNotice') -- This is line 16
        local Toggle = collectnotice.Toggle
        local text = Toggle.Text
        local value = text.Value
        value = "The Easy Egg!"
        person:WaitForChild('PlayerGui'):FindFirstChild('ScreenGui'):FindFirstChild('CollectionNotice').Toggle.Color.Value = Color3.fromRGB(0,255,0)
        person:WaitForChild('PlayerGui'):FindFirstChild('ScreenGui'):FindFirstChild('CollectionNotice').Toggle.Value = true
        script.Parent:Destroy()
        wait(5)
        game.Workspace.Variables.IfFirstEggInGame.Value = false
    end
end)

我已经在这里呆了好几个小时了。不知道如何修复错误。 FE 开启,是的,它的名字是“ScreenGui”,它在“PlayerGui”里面

错误:Workspace.Part.Script:16:尝试索引本地“screengui”(零值)

【问题讨论】:

  • 哦,顺便说一句,它在 Studio 中完美运行,但在游戏中却不行。

标签: lua roblox


【解决方案1】:

来自 Roblox 手册:http://wiki.roblox.com/index.php?title=API:Class/Instance/FindFirstChild

说明:返回找到的第一个具有给定名称的孩子,或 nil 如果不存在这样的孩子。

所以似乎没有名为“ScreenGui”的孩子。

如果一个函数可能返回 nil,你必须正确处理它。盲目地索引可能的 nil 值是不好的做法。

【讨论】:

  • 另外,这些脚本的工作方式与 Roblox Studio 中应有的完全一样。 Roblox Studio 在客户端上运行,这可能意味着问题所在。
  • 在游戏中运行服务器端
  • "右键单击 StarterGui,将鼠标悬停在 Insert Object 上,然后单击 ScreenGui。"这是来自 Roblox 开发人员“GUI 简介”的引述,解释了如何插入 ScreenGui。因为这是我所做的,意味着我索引它的方式不正确。
  • 我想我明白了。问题是我使用的是脚本。我注意到当 Guis 只是客户端对象时,服务器端出现了错误。所以我的问题是我应该使用本地脚本时使用服务器端脚本。
  • @King Maniac 这很奇怪。据我所知,它应该可以使用普通脚本。将其设为本地脚本是否可以解决问题?
猜你喜欢
  • 1970-01-01
  • 2017-03-12
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 2012-12-13
相关资源
最近更新 更多