拆卸时删除屏幕GUI

已经就武器冷却时间的问题提了一个问题,并且我创建了一个屏幕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即可

点赞
用户2860267
用户2860267

以下是中文翻译并且保留原先的 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)
2019-04-23 21:48:19