【问题标题】:Functions are having a problem where the 'end' part is not working函数在“结束”部分不起作用时出现问题
【发布时间】:2020-06-09 21:42:48
【问题描述】:

出于某种原因,我认为错误消息告诉我要去掉括号还是添加括号,我很困惑。它总是告诉我应该有一些东西而不是支架,或者需要一个支架,这取决于我如何更改它。 代码如下:

local Ammo = MaxAmmo
local Reloading = false

script.Parent.Equipped:Connect(function(Mouse)
    Mouse.Icon = "rbxassetid://1008161057"
    local function Reload()
        Reloading = true
        Mouse.Icon = "rbxassetid://1008161057"
        wait(2)
        Ammo = MaxAmmo
        Reloading = false
        Mouse.Icon = "rbxassetid://1008161057"
    end
    script.Parent.Activated:Connect(function()
        if Ammo>0 and not Reloading then
            Ammo=Ammo-1
            if Mouse.Target.Parent:FindFirstChild("Humanoid") then
                if Mouse.Target == ("Head") then
                    print('head')
                    Mouse.Target.Parent.Humanoid:TakeDamage(40)
                else do
                    Mouse.Target.Parent.Humanoid:TakeDamage(20)
                    print('body')   
                end
            end
        elseif Reloading == false then
            Reload()
        end
        print(Ammo)
    end)
    --local Input = game:GetService("UserInputService")
    --Input.InputBegan:Connect(function(Key)
        --if Key.Keycode == Enum.KeyCode.r and Reloading == false and Ammo~=MaxAmmo then
            --Reload()
        --end 
    --end)
end)```

【问题讨论】:

    标签: lua roblox


    【解决方案1】:
    if Mouse.Target == ("Head") then
      print('head')
      Mouse.Target.Parent.Humanoid:TakeDamage(40)
    else do
      Mouse.Target.Parent.Humanoid:TakeDamage(20)
      print('body')   
    end
    

    end 添加到do 或将其删除。

    要修复此类错误,请计算所有需要结束和所有结束的关键字,看看您是否得到相同的数字。有文本编辑器可以帮助您解决这个问题

    【讨论】:

      【解决方案2】:

      您应该使用诸如 ZeroBraneStudio 或 IntelliJ Community(带有 EmmyLua)之类的 IDE

      第一期:

      if Mouse.Target.Parent:FindFirstChild("Humanoid") then
                      if Mouse.Target == ("Head") then
                          print('head')
                          Mouse.Target.Parent.Humanoid:TakeDamage(40)
                      else do
                          Mouse.Target.Parent.Humanoid:TakeDamage(20)
                          print('body')   
                      end
                  end
      

      您应该从else do 中删除do

      第二期:

      你会从这里得到“意外的)”:

                  print(Ammo)
              end)
      

      这是因为你错过了if Ammo > 0 and not Reloading then的结尾

      生成的代码应如下所示:

      local Ammo = MaxAmmo
      local Reloading = false
      
      script.Parent.Equipped:Connect(function(Mouse)
          Mouse.Icon = "rbxassetid://1008161057"
          local function Reload()
              Reloading = true
              Mouse.Icon = "rbxassetid://1008161057"
              wait(2)
              Ammo = MaxAmmo
              Reloading = false
              Mouse.Icon = "rbxassetid://1008161057"
          end
          script.Parent.Activated:Connect(function()
              if Ammo > 0 and not Reloading then
                  Ammo = Ammo - 1
                  if Mouse.Target.Parent:FindFirstChild("Humanoid") then
                      if Mouse.Target == ("Head") then
                          print('head')
                          Mouse.Target.Parent.Humanoid:TakeDamage(40)
                      else
                          Mouse.Target.Parent.Humanoid:TakeDamage(20)
                          print('body')
                      end
                  elseif Reloading == false then
                      Reload()
                  end
                  print(Ammo)
              end
          end)
          --local Input = game:GetService("UserInputService")
          --Input.InputBegan:Connect(function(Key)
          --if Key.Keycode == Enum.KeyCode.r and Reloading == false and Ammo~=MaxAmmo then
          --Reload()
          --end
          --end)
      end)
      

      【讨论】:

      • 这篇文章很长,指出了一个多余的do
      • 没有第二期。错误消息是一个红鲱鱼,由实际问题引起。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-15
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      相关资源
      最近更新 更多