我的通知系统出现了“尝试调用实例值”的错误

这是代码的两部分:服务器和本地。我正在尝试制作一个系统,将用户的余额从表中存储在其玩家的IntValue中(位于“game.Players”中),然后稍后调用它以检查其是否已更改。如果发生任何事情,通知将通过本地脚本发送到GUI。无论发生什么,通知都不会出现。如果您需要其他东西,我很乐意提供。感谢您的阅读和任何潜在的帮助。这是一个名为Roblox上的Opix Islands游戏,在此我不是专家也不是新手,我只是有点卡住了。

服务器端:

    while true do
    for i,v in game.Players do --这是出现错误的行。

            local tab = nil
            for I,V in pairs(_G.GlobalData) do
                if V.UserId == v.UserId then
                    tab = V
                end
            end
            if not v.Bank then
                newval = Instance.new("IntValue",v)
                newval.Name = "Bank"
                wait()
                newval.Value = tab.Bank

            else
                if tab.Bank > v.Bank.Value then
                    print ("Greater than stored value")
                    local changedby = tab.Bank - v.Bank.Value
                    game.ReplicatedStorage.Notif.Return:FireClient(v,changedby,false)
                elseif tab.Bank < v.Bank.Value then
                    print ("Less than stored value")
                    local changedby = v.Bank.Value - tab.Bank
                    game.ReplicatedStorage.Notif.Return:FireClient(v,changedby,true)--Player to send to,amount it has changed by,If it has gone down
                end
            end

        end

    wait()
    end

和本地脚本:

game.ReplicatedStorage.Notif.Return.OnClientEvent:Connect(function(Change,Down)
     if Down == false then
        local Notification = script.Parent.NotificationExample:Clone()
        Notification.Name = "Notification"
        Notification.Parent = script.Parent
        Notification.Text = "您的银行账户余额刚刚增加了: £"..Change
        print ("Bank account Increased by:"..Change)
        Notification.Visible = true
        print ("通知应该可见")
        wait(3)
        Notification:Destroy()
        print ("通知应该消失了!")
    elseif Down == true then
            local Notification = script.Parent.NotificationExample:Clone()
            Notification.Parent = script.Parent
            Notification.Name = "Notification"
            Notification.Text = "您的银行账户余额刚刚减少了: £"..Change
            print ("Bank account decreased by:"..Change)
            Notification.Visible = true
            print ("通知应该可见")
            wait(3)
            Notification:Destroy()
            print ("通知应该消失了!")



    end


end)

这是一个层次结构图片,事件在复制存储中。 hierarchy

非常感谢。

点赞
用户2858170
用户2858170

看起来 game.Players 不是一个函数。

像这样

for i,v game.Players do

end

是行不通的。

根据 Lua 手册

像这样的 for 语句

 for var_1, ···, var_n in explist do body end

变量名 var_i 声明了循环变量,限定在循环体内。其中第一个变量是控制变量。循环从计算 explist 开始,生成四个值:一个迭代器函数、一个状态、一个控制变量的初始值和一个终止值。

然后,在每次迭代中,Lua 使用两个参数调用迭代器函数:状态和控制变量。

在你的情况下,Lua 将把 game.Players 视为迭代器函数,因为这是你提供的 explist 中的第一个(也是唯一的)元素。当调用它时,它会导致观察到的错误,因为它不是函数值。

你可能想要做这样的事情:

for i,v in ipairs(game.Player:GetChildren()) do end
2021-01-02 12:50:41
用户13578703
用户13578703
while true do
   for i,v in pairs(game.Players:GetChildren()) do --这行有错误

      local tab = nil
      for I,V in pairs(_G.GlobalData) do
         if V.UserId == v.UserId then
            tab = V
         end
      end
      if not v.Bank then
         newval = Instance.new("IntValue",v)
         newval.Name = "Bank"
         wait()
         newval.Value = tab.Bank

      else
         if tab.Bank > v.Bank.Value then
            print ("Greater than stored value")
            local changedby = tab.Bank - v.Bank.Value
            game.ReplicatedStorage.Notif.Return:FireClient(v,changedby,false)
         elseif tab.Bank < v.Bank.Value then
            print ("Less than stored value")
            local changedby = v.Bank.Value - tab.Bank
            game.ReplicatedStorage.Notif.Return:FireClient(v,changedby,true)--玩家送到,它已改变的数量,如果它已下降
         end
      end

   end

   wait()
end
2021-01-02 13:38:36