首页 > 解决方案 > 为什么我不断收到错误消息“尝试在我的脚本中使用 'Cash' 索引 nil?

问题描述

对于一些上下文,我正在 Roblox 上制作一个 Tycoon 来测试我在 lua 方面的知识。在我尝试制作leaderstats之前,一切都很好。第一个脚本,也就是 Tycoon 的功能,在这里:



     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

标签: luaroblox

解决方案


local name = game.Players:GetChildren()

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

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

game.Players 是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正确使用以及此类事情。

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


推荐阅读