【问题标题】:Roblox - attempt to index field '?' (a nil value) Issue with running a module functionRoblox - 尝试索引字段“?” (零值)运行模块功能的问题
【发布时间】:2019-06-06 22:03:10
【问题描述】:

我正在尝试创建数据存储并更改玩家数据的值,但在获取脚本以运行该函数时遇到问题。我将粘贴我遇到问题的代码并标记出现错误的行。对于任何新手的错误,我深表歉意,这将是我第一次使用 Lua 和 Roblox 编写游戏。我认为这可能是我如何调用statName 以及"Wheat" 的statName 不存在的问题,但我不知道如何调用它,或者为什么它不存在。

这是模块脚本中的相关内容:

function PlayerStatManager:ChangeStat(player, statName, value)

    local playerUserId = "Player_" .. player.UserId
    assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")  <--this line
    if typeof(sessionData[playerUserId][statName]) == "number" then
        sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
    else
        sessionData[playerUserId][statName] = value
    end

end


-- Function to add player to the 'sessionData' table

local function setupPlayerData(player)

    local playerUserId = "Player_" .. player.UserId
    local data
    local success, err = pcall(function()
        playerData:UpdateAsync(playerUserId, function(playerData)
            data = playerData
        end)
    end)

    if success then
        if data then
            -- Data exists for this player
            sessionData[playerUserId] = data
        else
            -- Data store is working, but no current data for this player
            sessionData[playerUserId] = {Money=0, Wheat=0, Silo=0, Feeders=0, Chickens=0}
        end
    else
        warn("Cannot set up data for player!")
    end
end

这是使用模块脚本的脚本中的相关内容:

local SrvrStats = require(game.ServerStorage.moduleScript)

SrvrStats:ChangeStat(player, 'Wheat', playerWheat.Value) <-- this line

【问题讨论】:

  • 错误信息的意思是sessionData[playerUserId]是nil
  • 可能在ChangeStat 调用之前没有调用setupPlayerData
  • 你是对的,当我测试它时,将启动 setupPlayerData 的 playerAdded 命令没有触发,因为显然 :GetAsync 和 :UpdateAsync 方法需要一段时间才能执行,这会阻止它从知道它应该在玩家加入之前触发。谢谢。
  • 能否请您展示完整的模块?我看不到模块何时触发 setupPlayerData 函数。而且,模块返回什么?

标签: lua roblox


【解决方案1】:

试试这个:

local PlayerStatManager = {}

PlayerStatManager.ChangeStat = function(player, statName, value)

    local playerUserId = "Player_" .. player.UserId
    assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")  <--this line
    if typeof(sessionData[playerUserId][statName]) == "number" then
        sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
    else
        sessionData[playerUserId][statName] = value
    end

end


-- Function to add player to the 'sessionData' table

PlayerStatManager.setupPlayerData = PlayerStatManager(player)

    local playerUserId = "Player_" .. player.UserId
    local data
    local success, err = pcall(function()
        playerData:UpdateAsync(playerUserId, function(playerData)
            data = playerData
        end)
    end)

    if success then
        if data then
            -- Data exists for this player
            sessionData[playerUserId] = data
        else
            -- Data store is working, but no current data for this player
            sessionData[playerUserId] = {Money=0, Wheat=0, Silo=0, Feeders=0, Chickens=0}
        end
    else
        warn("Cannot set up data for player!")
    end
end

return PlayerStatManager

【讨论】:

    猜你喜欢
    • 2012-03-12
    • 2020-05-28
    • 1970-01-01
    • 2019-10-10
    • 2013-06-18
    • 2011-11-05
    • 2018-12-30
    相关资源
    最近更新 更多