拆卸时删除屏幕GUI
2019-4-23 14:38:4
收藏:0
阅读:106
评论:1
我已经就武器冷却时间的问题提了一个问题,并且我创建了一个屏幕GUI来显示玩家何时可以再次射击,实现相同的Debounce代码。 问题是,我不知道如何从屏幕中删除screengui / textlabel。由于我打算制作的每个工具都有自己的GUI,如果一个工具的screenGUI不删除,它将覆盖相同工具的GUI / 其他工具的GUI。
我已经尝试过像this question中所述的隐藏文本标签,如下所示
player.PlayerGui.ScreenGui.TextLabel.Visible = false
但是
1)它只在第一次取消佩戴时消失,
2)我担心它不会被删除,而是被隐藏,经过一段时间,堆叠的隐藏GUI将以某种方式影响游戏的平滑度。
local player =游戏玩家.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent
--当玩家时创建一个文本标签,其中包含文本块已准备好! 时
local function onEquip()
print("screengui1")
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = player.PlayerGui
local textLabel = Instance.new("TextLabel")
textLabel.Parent = screenGui
textLabel.BackgroundTransparency = 0.85
textLabel.Position = UDim2.new(0, 1100, 0, 550)
textLabel.Size = UDim2.new(0, 150, 0, 50)
textLabel.BackgroundColor3 = BrickColor.White().Color
textLabel.Text = "Block ready!"
end
local isGunOnCooldown = false
local cooldownTime = 3 --seconds
mouse.Button1Down:Connect(function()
-- debounce clicks so the text dont changes (its cooldown is the same as the gun cooldown the GUI is within)
if isGunOnCooldown then
return
end
-- change the text,
isGunOnCooldown = true
player.PlayerGui.ScreenGui.TextLabel.Text = "Reloading..."
-- start the cooldown and reset it along with the test
spawn(function()
wait(cooldownTime)
isGunOnCooldown = false
player.PlayerGui.ScreenGui.TextLabel.Text = "Block ready!"
end)
end)
local function onUnequip()
--在此处编写删除GUI的代码
end
tool.Equipped:connect(onEquip)
tool.Unequipped:connect(onUnequip)
我只需要解释如何删除包含玩家取消佩戴武器时向其显示的textlabel的screenGUI即可
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 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 代码?

以下是中文翻译并且保留原先的 markdown 格式:
function createAmmoUI() -- 当玩家准备好时,创建一个文本标签,并显示文本“Block Ready!” local screenGui = Instance.new("ScreenGui") local textLabel = Instance.new("TextLabel") textLabel.Name = "Message" textLabel.Parent = screenGui textLabel.BackgroundTransparency = 0.85 textLabel.Position = UDim2.new(0, 1100, 0, 550) textLabel.Size = UDim2.new(0, 150, 0, 50) textLabel.BackgroundColor3 = BrickColor.White().Color textLabel.Text = "Block ready!" return screenGui end -- 创建一个持久化的 UI 元素以显示剩余子弹数和重新装载完成的时间 local ammoUI = createAmmoUI() local function onEquip() -- 当工具被装备时,也显示 UI print("将工具装备到当前播放器") ammoUI.Parent = player.PlayerGui end local function onUnequip() -- 当工具被取消装备时,也将 UI 从屏幕上完全移除 print("取消装备工具 UI") ammoUI.Parent = nil end local isGunOnCooldown = false local cooldownTime = 3 -- 秒 -- 防抖动,避免文字更改(其冷却时间与枪冷却时间相同,GUI 之中) mouse.Button1Down:Connect(function() if isGunOnCooldown then return end -- 改变文字, isGunOnCooldown = true ammoUI.Message.Text = "重新装填..." -- 开始冷却并重置它以及测试 spawn(function() wait(cooldownTime) isGunOnCooldown = false ammoUI.Message.Text = "Block ready!" end) end) -- 当工具被装备/取消装备时,分别连接到 onEquip/onUnequip 函数 tool.Equipped:connect(onEquip) tool.Unequipped:connect(onUnequip)