为什么我不断收到错误消息“试图在脚本中使用'Cash'索引nil?

问题描述 投票:1回答:1

出于某种背景,我在Roblox上制作了一个大亨来测试我对lua的了解。一切工作正常,直到我尝试成为Leaderstats。第一个脚本,就是制作大亨的原因功能,在这里:



     local TycoonInfo = script.Parent:WaitForChild("TycoonInfo")
        local OwnerValue = TycoonInfo:WaitForChild("Owner")
        local Buttons = script.Parent:WaitForChild("Buttons")
        local Purchases = script.Parent:WaitForChild("Purchases")

        local Objects = {}

        function validateHitByOwner(Hit)
            if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
                local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
                if Player and OwnerValue.Value==  Player then
                    return true
                end
            end
            return false
        end

        for i,v in pairs(Buttons:GetChildren()) do
            local Object = Purchases:FindFirstChild(v.Object.Value)
            if Object then
                Objects[Object.Name] = Object:Clone()
                Object:Destroy()

                if v:FindFirstChild("Dependency") then
                    coroutine.resume(coroutine.create(function()
                        v.Button.Transparency = 1
                        v.Button.BillboardGui.Enabled = false
                        v.Button.CanCollide = false
                        if Purchases:WaitForChild(v.Dependency.Value,90000) then
                            v.Button.Transparency = 0
                            v.Button.CanCollide = true
                            v.Button.BillboardGui.Enabled = true
                        end
                    end))
                end
                local DB = false
                v.Button.Touched:Connect(function(Hit)
                    if validateHitByOwner(Hit)then
                        if DB == false then
                            DB = true
                            if v.Button.Transparency == 0 then
                                local Purchased = DoPurchase(Hit,v,v.Price.Value,Objects[v.Object.Value])
                                if Purchased then
                                    warn("Purchased")
                                else
                                    warn("Can't purchase")
                                end
                            end
                            DB = false
                        end
                    end
                end)
            end
        end

        function DoPurchase(Hit,Button,Cost,Object)
            if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
                local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
                if Player and OwnerValue.Value == Player then
                    local name = game.Players:GetChildren()
                    local CashValue = game.Players.name.leaderstats.Cash -- Error here
                    if CashValue then
                        if CashValue.Value >= Cost then
                            CashValue.Value = CashValue.Value - Cost
                            Object.Parent = Purchases
                            Button:Destroy()
                            return true 
                        end 
                    end
                end
            end
            return false
        end

下一个脚本将成为排行榜并分配一个最近加入随机可用大亨的玩家,在这里:



     function FindEmptyTycoon()
            for i,v in pairs(workspace.Tycoons:GetChildren()) do
                if v.TycoonInfo.Owner.Value == nil then
                    return v
                end
            end
            return nil
        end


        game.Players.PlayerAdded:Connect(function(Player)
            local Tycoon = FindEmptyTycoon()
            Tycoon.TycoonInfo.Owner.Value = Player 


            local stats = Instance.new("Folder",Player)
            stats.Name = "leaderstats"

            local Cash = Instance.new("IntValue",stats)
            Cash.Name = "Cash"
        end)

然后,存储在所有可购买物品中以给玩家钱的脚本在这里:


function giveCash(player)
    wait(3.33)
    local Cash = player.leaderstats.Cash
    Cash.Value = Cash.Value + 2
end

顺便说一下,这些脚本是我一直在尝试编辑的原始脚本的版本,以便它们可以实际运行,我想我已经快要纠正错误了。原始脚本可以在这里找到(按出现顺序):

local TycoonInfo = script.Parent:WaitForChild("TycoonInfo")
local OwnerValue = TycoonInfo:WaitForChild("Owner")
local Buttons = script.Parent:WaitForChild("Buttons")
local Purchases = script.Parent:WaitForChild("Purchases")

local Objects = {}

function validateHitByOwner(Hit)
    if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
        if Player and OwnerValue.Value==  Player then
            return true
        end
    end
    return false
end

for i,v in pairs(Buttons:GetChildren()) do
    local Object = Purchases:FindFirstChild(v.Object.Value)
    if Object then
        Objects[Object.Name] = Object:Clone()
        Object:Destroy()

        if v:FindFirstChild("Dependency") then
            coroutine.resume(coroutine.create(function()
                v.Button.Transparency = 1
                v.Button.BillboardGui.Enabled = false
                v.Button.CanCollide = false
                if Purchases:WaitForChild(v.Dependency.Value,90000) then
                    v.Button.Transparency = 0
                    v.Button.CanCollide = true
                    v.Button.BillboardGui.Enabled = true
                end
            end))
        end
        local DB = false
        v.Button.Touched:Connect(function(Hit)
            if validateHitByOwner(Hit)then
                if DB == false then
                    DB = true
                    if v.Button.Transparency == 0 then
                        local Purchased = DoPurchase(Hit,v,v.Price.Value,Objects[v.Object.Value])
                        if Purchased then
                            warn("Purchased")
                        else
                            warn("Can't purchase")
                        end
                    end
                    DB = false
                end
            end
        end)
    end
end

function DoPurchase(Hit,Button,Cost,Object)
    if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
        if Player and OwnerValue.Value == Player then
            local CashValue = game.ServerStorage.PlayerCash:FindFirstChild(Player.Name)
            if CashValue then
                if CashValue.Value >= Cost then
                    CashValue.Value = CashValue.Value - Cost
                    Object.Parent = Purchases
                    Button:Destroy()
                    return true
                end
            end
        end
    end
    return false
end
function FindEmptyTycoon()
    for i,v in pairs(workspace.Tycoons:GetChildren()) do
        if v.TycoonInfo.Owner.Value == nil then
            return v
        end
    end
    return nil
end


game.Players.PlayerAdded:Connect(function(Player)
    local Tycoon = FindEmptyTycoon()
    Tycoon.TycoonInfo.Owner.Value = Player


    local stats = Instance.new("BoolValue",Player)
    stats.Name = "leaderstats"
    stats.Parent = game.ServerStorage

    local Cash = Instance.new("IntValue",stats)
    Cash.Name = Player.Name
    Cash.Value = 0
    Cash.Parent = game.ServerStorage.PlayerCash
end)
local amount = 2
local timedelay = 2.33
local currencyname = "Cash"
wait(1)
while true do
    wait(timedelay)
    for i,v in pairs(game.ServerStorage.PlayerCash:GetChildren()) do
        if v:FindFirstChild("leaderstats") and v then
            v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + amount
        end
    end
end
lua roblox
1个回答
1
投票
local name = game.Players:GetChildren()

为什么将玩家的孩子的列表存储在名为name的变量中?此函数返回一个表。名称将更适合于字符串。

local CashValue = game.Players.name.leaderstats.Cash -- Error here

[game.Player s是https://developer.roblox.com/en-us/api-reference/class/Players] >>

我无法理解game.Players.name,甚至无法理解game.Players.name.leaderstats

您是说Player.leaderstats吗?

在遇到Players.name.leaderstats的错误之前,我希望game.Players.name.leaderstats.Cash的错误消息

name不是播放器的属性,因此不应允许您索引Players.name

我不是Roblox专家,但是从我的角度来看,您的代码是如此之差,您真的应该从简单的教程开始,而不是修复此代码。

我认为您应该重新开始,并确保您了解如何正确使用Instance.new等。

使用更好的变量名也有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.