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)
你可能需要注意的是,如果全局变量是在函数声明时声明的,那么如果在单独的线程中声明它,可能仍然为 nil。
事实上,所给出的错误代码与您提供的代码片段无关。
如果你提供了整个模块和你的声明,那么我们可以直接在这里进行修补……但在那之前,只要确保你的声明在线程之间同步就好了。
正确的方法是使用模块,因为 _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 和充足的空闲时间,而现在我的时间不多,希望我的回答能为您提供帮助。
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

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)