取消装备时删除 screenGUI

Remove a screenGUI when unequipping

和我制作了一个 screenGUI 以显示玩家何时可以再次射击,实现相同的去抖动代码。问题是我不知道如何从屏幕上删除 screengui/textlabel。由于我打算做的每个工具都有自己的 GUI,如果一个工具的 screenGUI 不删除,它将与同一工具的 GUI/其他工具的 GUI 重叠。

我已经尝试像这样隐藏 中所述的文本标签 player.PlayerGui.ScreenGui.TextLabel.Visible = false 但是 1)它只会让它在第一次未装备时消失并且 2)我担心它不会被删除,而是被隐藏,一段时间后,堆叠的隐藏GUI会以某种方式影响游戏的流畅性。

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent

--Creaets a text label with the text Block Ready! on it when the player 
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()
    --code to delete the gui goes here
end
tool.Equipped:connect(onEquip)
tool.Unequipped:connect(onUnequip)

我只需要解释一下如何删除包含玩家卸下武器时显示给玩家的文本标签的 screenGUI

处理此问题的最简单方法是在首次创建 UIElement 时保留对 UIElement 的引用。然后,当您装备该工具时,您只需设置 Parent。当您取消装备时,您将 Parent 设置为 nil。这样一来,您就知道总会有一个元素,您只需控制它何时可见。

local function createAmmoUI()
    --Creates a text label with the text Block Ready! on it when the player
    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

-- make a persistent UI element to show off how much ammo is left and when reload is done
local ammoUI = createAmmoUI()


local function onEquip()
    -- when the tool is equipped, also show the UI
    print("Equipping Tool to current Player")
    ammoUI.Parent = player.PlayerGui
end

local function onUnequip()
    -- when the tool is unequipped, also remove the UI from the screen entirely
    print("Unequiping Tool UI")
    ammoUI.Parent = nil
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
    ammoUI.Message.Text = "Reloading..."

    -- start the cooldown and reset it along with the test
    spawn(function()
        wait(cooldownTime)
        isGunOnCooldown = false
        ammoUI.Message.Text = "Block ready!"
    end)
end)

tool.Equipped:connect(onEquip)
tool.Unequipped:connect(onUnequip)