繁体   English   中英

尝试使用“leaderstats”索引 nil

[英]Attempt to index nil with 'leaderstats'

所以基本上我想制作一个塔防游戏,我添加了一个按钮,点击它会在带有 AI 的塔/机器人中生成。 (脚本是本地的)尝试使用 'leaderstats' 索引 nil总是让我出错。 (我知道这意味着游戏不知道leaderstats 是什么),我制作了一个脚本,在播放器内部创建了名为leaderstats 的文件夹,里面有硬币和其他东西,但它仍然不知道有一个leaderstats 文件夹。 我也试过 FindFirstChild() 但它只是说Attempt to index nil with 'FindFirstChild' 谁能帮我?

local button = script.Parent

button.MouseButton1Click:Connect(function(player)
        local spawner = game.Workspace.ts1
    local money = player.leaderstats.Cash.Value
        if money >= 250 then
            money = money - 250
            local clone = game.ReplicatedStorage.Allies.Guard:Clone()
            clone.Parent = workspace
            clone.HumanoidRootPart.CFrame = spawner.HumanoidRootPart.CFrame
        else
            if money <= 250 then
            button.BackgroundColor3 = Color3.new(1, 0, 0)
            button.Text = "Too Expensive"
            wait(0.5)
            button.Text = "Guard [250$]"
                button.BackgroundColor3 = Color3.new(0.603922, 0.603922, 0.603922)

            end
        end
    end)

GuiButtons 上的MouseButton1 事件不提供任何 arguments,因此您的player变量最终为零。 当您尝试说player.leaderstats时,它会引发错误,因为 player 没有任何子项或属性要索引,因为它是 nil。 正如小猪指出的那样。

但由于这是一个 LocalScript,您可以使用Players.LocalPlayer object 轻松访问 Player object。

local button = script.Parent
button.MouseButton1Click:Connect( function()
    local player = game.Players.LocalPlayer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM