ROBLOX Lua - 图片透明度功能

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

_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)

点赞
用户7134804
用户7134804
local player = game.Players.LocalPlayer
local Image = player.StarterGui.Fade.FadeImage
local i = 0 --将 i 当作 repeat 函数的停止器,避免它超出范围。
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)
2016-11-09 04:22:40
用户7452073
用户7452073

你可能需要注意的是,如果全局变量是在函数声明时声明的,那么如果在单独的线程中声明它,可能仍然为 nil。

事实上,所给出的错误代码与您提供的代码片段无关。

如果你提供了整个模块和你的声明,那么我们可以直接在这里进行修补……但在那之前,只要确保你的声明在线程之间同步就好了。

2017-01-22 04:25:13
用户88888888
用户88888888

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

例如:

在服务器脚本中:

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

在客户端脚本中:

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

结果:

服务器:尝试索引布尔 'topkek':空值
客户端:尝试索引布尔 'kek':空值

如果运行此代码,将抛出错误,因为它不在服务器和客户端之间共享,而是在客户端之间和服务器之间共享。

例如:

在 Server Script 1 中:

_G.kek = true;
wait(.1); -- 为了安全增加一些延迟时间
print(_G.topkek);

在 Server Script 2 中:

_G.topkek = false;
wait(.1); -- 同上
print(_G.kek);

结果:

服务器脚本1: 'false'
服务器脚本2: 'true'

不仅如此,您还使用了 game.StarterGui 而不是使用玩家的 PlayerGui。此外,您还使用了 'repeat',但没有 wait()。使用任何类型的循环(while true do,repeat until,for i in math.huge)而没有 wait() 将导致您的游戏冻结和崩溃。

因此,为了澄清所有其他内容,这是您应该做的:

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

local api = {};

api.BeginFade = function(image)
    local alpha = 30; -- 在图像完全透明之前循环的次数。较高的数字表示图像淡入和淡出需要更长的时间。
    image.Visible = true;
    image.ImageTransparency = 1;

    for i = 1, alpha do
        image.ImageTransparency = image.ImageTransparency - 1/alpha;
        wait(); -- 在这里使用 'wait()' 以防止循环在 0 秒后结束。
    end;
    wait(0.7); -- 我曾经使用过具有此类屏幕的游戏,我相信 0.7 秒比 2 秒更好。相信我,我有200个游戏。哈哈
    for i = 1, alpha do
        image.ImageTransparency = image.ImageTransparency + 1/alpha;
        wait();
    end;
    image.ImageTransparency = 1; -- 不是必要的,但作为 '额外检查' 有效。
    return image; -- 我们必须返回一些东西,对吧?(不是必要的)
end;

return api;

在传送板内的脚本中:

local pad = script.Parent;
local rep = game:service("ReplicatedStorage");
local fade = rep:WaitForChild("fade");
fade = require(fade); -- 提取包含函数的模块表

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); -- 添加一点冷却时间
            end;
        end;
    end;
end);

这在大多数情况下就可以了。但是,exploiter 可能能够删除 Fade 模块,但是修复这个问题需要使用 RemoteEvents 和充足的空闲时间,而现在我的时间不多,希望我的回答能为您提供帮助。

2018-11-11 03:59:23