【问题标题】:Why would this script not work?为什么这个脚本不起作用?
【发布时间】:2015-06-16 16:53:31
【问题描述】:

以下脚本在 print("2") 和 print("3") 之间中断,因为它找不到变量“tag”。我该如何解决这个问题?

local Humanoid = script.Parent.Zombie -- Or Zombie Or Whatever 
function PwntX_X() 
    print("1")
    local tag = Humanoid:findFirstChild("creator")
    print("2") 
    if tag ~= nil then 
        print("3")
        if tag.Value ~= nil then 
            print("4")

            local Leaderstats = tag.Value:findFirstChild("leaderstats") 
            print("5")
            if Leaderstats ~= nil then 
                print("6")
                Leaderstats.Cash.Value = Leaderstats.Cash.Value + 5
                print("7")
                wait(0.1)`enter code here`
                script:remove()
            end 
        end 
    end 
end 
Humanoid.Died:connect(PwntX_X) 

我已经有一个 100% 有效的排行榜脚本。该脚本用于名为“ROBLOX”的游戏。谢谢!

【问题讨论】:

  • 更多的 Lua 方法来检查 not nil 是:if not tag then ... end
  • 好的,我会改的。
  • 为了回答您的实际问题,函数 findFirstChild("creator") 返回 nil,因此您必须发布该函数的代码以获得帮助。
  • 关于not tag vs tag ~= nilnilfalse是假的,只要你不需要区分它们,not tag更干净。

标签: lua roblox


【解决方案1】:

好吧,首先,scriptinghelpers.org 是专门为 Roblox 创建的,类似于 stackoverflow 的服务,我建议下次,你在那里问,现在开始;

-- If you have further problems, My Roblox username is 'ZeroBits' (REMOVE THIS LINE)

DEBUG = true -- Debug Print, so you can disable it when you're done.
local function print_D(t)
    if DEBUG == true then warn(t) end end
print_D("DEBUG MODE, Switch Debug Value to False when finished")

local Humanoid = script.Parent:FindFirstChild("Zombie") -- We'll use FindFirstChild() here
--if not Humanoid then -- Uncomment this statement if you want it to affect objects named Humanoid as well
--    Humanoid = script.Parent:FindFirstChild("Humanoid")
--end
function HumanoidKilled() -- Renamed the function to be a little less cringeworthy 
    print_D("1") -- switched these print statements with the Print_D() function
    local tag = Humanoid:FindFirstChild("creator") -- Capitalized the first letter in FindFirstChild()
    print_D("2") 
    if not tag then 
        warn("No Tag Found check Humanoid and weapon script")  --if this prints, there's either no tag, or a serious problem, and you should check the humanoid object for a tag, or make sure the weapons you use actually generate tags.
    else -- changed 'if tag ~= nil then' to 'if not tag then 'do stuff' else' to simplify the code, and add an else statement early on.
        print_D("3")
        if tag.Value then -- removed '~= nil' because it's just useless clutter
            print_D("4")
            local Leaderstats = tag.Value:findFirstChild("leaderstats") 
            print_D("5")
            if Leaderstats ~= nil then 
                print_D("6")
                Leaderstats.Cash.Value = Leaderstats.Cash.Value + 5
                print_D("7")
                wait(0.1)
                script:Destroy() -- switched remove() to Destroy(), remove() is deprecated
            end 
       end 
    end
end 
Humanoid.Died:connect(HumanoidKilled) 

这修复了脚本中的所有问题,并提高了效率。如果还有问题,要么是在标签创建脚本中,标签没有存储在人形机器人中,要么标签没有命名为'creator'

另外,我将 print 语句切换到 print_D 函数,它使用 warn() 而不是 print() 几乎没有区别,只是调试文本在控制台中将显示为黄色,而不是白色。

脚本正在用于名为“ROBLOX”的游戏。谢谢!

这是针对 Roblox 上的游戏,而不是 Roblox 游戏,您所说的类似于在说;我为 Source Engine 制作了这个模组,而你实际上为半条命 2 制作了模组

【讨论】:

    猜你喜欢
    • 2020-12-14
    • 2013-10-20
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 2018-10-25
    • 2021-08-29
    相关资源
    最近更新 更多