【问题标题】:Roblox Studio Lua Problem "Workspace.Ultimate.Script:12: attempt to compare number < string"-Roblox Studio Lua 问题“Workspace.Ultimate.Script:12:尝试比较数字 < 字符串”-
【发布时间】:2021-08-15 06:38:41
【问题描述】:
deb = true
giv = script.Parent.Giver
function touch(part)
    local hum = part.Parent:FindFirstChild("Humanoid")
    if hum then
        local plr = game.Players:FindFirstChild(part.Parent.Name)
        local ls = plr:FindFirstChild("leaderstats")
        local cash = ls:FindFirstChild("Cash")
        if plr then
            if deb == true then
                if cash.Value > 12500 then
                deb = false
                giv.BrickColor = BrickColor.new("Really red")
                    local weapon = game.ReplicatedStorage.Jutsus:FindFirstChild("Fireball")
                local w2 = weapon:Clone()
                w2.Parent = plr.Backpack
                wait(script.Parent.RegenTime.Value)
                giv.BrickColor = BrickColor.new("Bright violet")
                    deb = true
                end
            end
        end
    end
end
script.Parent.Giver.Touched:connect(touch) 

我被困在这一点上,我尝试了很多东西,但没有任何工作输出是这样的:

Workspace.Ultimate.Script:12: attempt to compare number < string  -  Server - Script:12

如果有人为我改进代码,我将不胜感激。

【问题讨论】:

    标签: lua compare roblox


    【解决方案1】:

    显然cash.Value 是一个字符串,所以你不能使用cash.Value &gt; 12500

    cash.Value 转换为数字,然后再与数字进行比较。

    tonumber(cash.Value) &gt; 12500.

    此解决方案假定cash.Value 是一个表示数字的字符串。如果不是这种情况,您必须先确保这一点。

    【讨论】:

      【解决方案2】:

      字符串和数字比较需要提前转换,并且需要确保casg.Value必须是数字,否则可能会提示与nil值进行比较

      那你就可以这样写代码了。

      deb = true
      giv = script.Parent.Giver
      function touch(part)
          local hum = part.Parent:FindFirstChild("Humanoid")
          if hum then
              local plr = game.Players:FindFirstChild(part.Parent.Name)
              local ls = plr:FindFirstChild("leaderstats")
              local cash = ls:FindFirstChild("Cash")
              if plr then
                  if deb == true then
                      if (tonumber(cash.Value) or 0) > 12500 then
                          deb = false
                          giv.BrickColor = BrickColor.new("Really red")
                          local weapon = game.ReplicatedStorage.Jutsus:FindFirstChild("Fireball")
                          local w2 = weapon:Clone()
                          w2.Parent = plr.Backpack
                          wait(script.Parent.RegenTime.Value)
                          giv.BrickColor = BrickColor.new("Bright violet")
                          deb = true
                      end
                  end
              end
          end
      end
      script.Parent.Giver.Touched:connect(touch) 
      

      【讨论】:

        猜你喜欢
        • 2020-10-19
        • 1970-01-01
        • 2021-07-04
        • 1970-01-01
        • 2013-05-30
        • 1970-01-01
        • 1970-01-01
        • 2019-05-04
        相关资源
        最近更新 更多