【问题标题】:ROBLOX Lua - Image Transparency FunctionROBLOX Lua - 图像透明功能
【发布时间】:2017-03-21 03:16:47
【问题描述】:

我正在尝试这样当你触摸砖块时,它会冻结你的角色,然后在 ModuleScript 中运行一个 函数 来生成 ImageLabel 当你的角色被传送到该建筑物的内部/外部时,它会慢慢出现然后消失。到目前为止,我已经设法让它冻结你的角色并调用该函数,但是使图像出现和消失的代码不起作用。这是代码:

_G.BeginFade = {}

_G.BeginFade.GlobalFunction = function()

`local Image = game.StarterGui.Fade.FadeImage`
Image.Visible = true
repeat
    Image.ImageTransparency = Image.ImageTransparency - 0.1
    wait(0.2)
until
    Image.ImageTransparency == 0
wait(2)
repeat
    Image.ImageTransparency = Image.ImageTransparency + 0.1
until
    Image.ImageTransparency == 1

end

我使用 _G.BeginFade.GlobalFunction() 调用该函数,并从不同的脚本调用它。包含该函数的 ModuleScript 位于 StarterGui 中。它返回此错误:

Workspace.Home Teleport.tele2.Teleport pad Script:47: attempt to index field 'BeginFade' (a nil value)

【问题讨论】:

    标签: function lua roblox


    【解决方案1】:
    local player = game.Players.LocalPlayer
    local Image = player.StarterGui.Fade.FadeImage
    local i = 0 --have i as a stopper for the repeat function, it tends to go over it.
    script.Parent.Door1.Touched:connect(function(hit)
    if hit.Humanoid ~= nil then
    Image.Visible = true
    repeat
    Image.Transparency = Image.Transparency - 0.1
    i = i + 1
    wait(0.1)
    until i == 10
    end
    end)
    
    script.Parent.Door2.Touched:connect(function(hit)
    if hit.Humanoid ~= nil then
    Image.Visible = true
    repeat
    Image.Transparency = Image.Transparency - 0.1
    i = i - 1
    wait(0.1)
    until i == 0 
    end
    end)
    

    【讨论】:

    • 谢谢,但我需要它作为一个函数,这样我就可以从其他脚本中调用它。如果我将它的一部分放入一个函数中,这会起作用吗?
    【解决方案2】:

    您可能需要研究的是,如果全局是在函数执行时声明的,因为如果您在单独的线程中声明它,它仍有可能为 nil。

    其实给出的错误码和你给的sn-p代码没有关系。

    如果您向我们提供了整个模块和您的声明,那么我们可以在此处为您提供直接补丁...但在那之前,请注意您的声明是否在线程之间同步。

    【讨论】:

      【解决方案3】:

      正确执行此操作的方法是使用模块,因为 _G 仅在特定组/类脚本之间共享。

      例如:

      在服务器脚本中:

      _G.kek = true;
      print(_G.topkek);
      

      在客户端脚本中:

      _G.topkek = false;
      print(_G.kek);
      

      结果:

      server: attempt to index boolean 'topkek': a nil value
      client: attempt to index boolean 'kek': a nil value
      

      如果你运行它,它会抛出一个错误,因为这不是在服务器和客户端边界之间共享的,但是它是在客户端到客户端和服务器到服务器之间共享的

      例如:

      在服务器脚本 1 中:

      _G.kek = true;
      wait(.1); -- add a bit of delay just in case
      print(_G.topkek);
      

      在服务器脚本 2 中:

      _G.topkek = false;
      wait(.1); -- same here
      print(_G.kek);
      

      结果:

      server script 1: 'false'
      server script 2: 'true'
      

      不仅如此,还在于您使用了 game.StarterGui 而不是使用 Player 的 PlayerGui。而且,您使用了“重复”,但没有等待()。在没有 wait() 的情况下使用任何类型的循环(如果为 true,则重复直到,for i in math.huge)会导致游戏冻结和崩溃。

      所以,要清除所有其他内容,您应该这样做:

      在名为“fade”的 ModuleScript 中,位于 ReplicatedStorage:

      local api = {};
      
      api.BeginFade = function(image)
          local alpha = 30; -- Change this to the number of cycles before fully transparency. Higher = longer time that the image will take to fade in and out.
          image.Visible = true;
          image.ImageTransparency = 1;
      
          for i = 1, alpha do
              image.ImageTransparency = image.ImageTransparency - 1/alpha;
              wait(); -- We use 'wait()' here to prevent the loop from ending in 0 seconds.
          end;
          wait(0.7); -- I've worked with games that have screens like this, and I believe 0.7 time is better than 2 seconds. Trust me, I've got 200 places. lol
          for i = 1, alpha do
              image.ImageTransparency = image.ImageTransparency + 1/alpha;
              wait();
          end;
          image.ImageTransparency = 1; -- Not necessary too, but works as a 'extra check'.
          return image; -- We gotta return something, right? (not necessary)
      end;
      
      return api;
      

      在 telport pad 内的脚本中:

      local pad = script.Parent;
      local rep = game:service("ReplicatedStorage");
      local fade = rep:WaitForChild("fade");
      fade = require(fade); -- Extract the table of the Module which contains functions
      
      pad.Touched:connect(function(ht)
          local hit = ht.Parent;
          if hit.ClassName == "Model" then
              if hit:FindFirstChildOfClass("Humanoid")then
                  local name = hit.Name;
                  local plr = nil;
      
                  for _,v in pairs(game:service("Players"):GetPlayers())do
                      if v.Name == name then
                          plr = v;
                      end;
                  end;
                  if plr ~= nil then
                      local img = plr.PlayerGui.Fade.FadeImage;
                      fade.BeginFade(img);
                      wait(0.1); -- Add a bit of cooldown
                  end;
              end;
          end;
      end);
      

      在大多数情况下应该这样做。虽然,利用者可以删除 Fade 模块,但修复它需要 RemoteEvents 和大量空闲时间,现在我的时间不多了,所以我希望我的回答对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多