Убрать скрин GUI при снятии экипировки - PullRequest
0 голосов
/ 23 апреля 2019

Я уже задал вопрос о перезарядке оружия , и я сделал экранный графический интерфейс, чтобы показать игроку, когда он может стрелять снова, применяя тот же код отказов.Проблема в том, что я понятия не имею, как удалить экранную метку / текстовую метку с экрана.Поскольку каждый инструмент, который я планирую использовать, имеет свой собственный графический интерфейс пользователя, если screenGUI одного инструмента не удаляется, он будет перекрываться с графическим интерфейсом того же инструмента / другого инструмента.

Я уже пытался скрыть текстпометить, как указано в этот вопрос как этот player.PlayerGui.ScreenGui.TextLabel.Visible = false но 1) он только заставляет его исчезнуть в первый раз, когда он не экипирован и 2) я боюсь, что после того, как он не будет удален, а скорее скрыт, через некоторое времяСложенные скрытые графические интерфейсы каким-то образом влияют на гладкость игры.

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)

Мне просто нужно объяснить, как удалить экранный графический интерфейс пользователя, который содержит текстовую метку, отображаемую игроку, когда они снимают оружие

1 Ответ

1 голос
/ 24 апреля 2019

Самый простой способ справиться с этим - сохранить ссылку на UIElement при первом его создании.Затем, когда вы надеваете инструмент, вы просто устанавливаете Родителя.Когда вы снимаете экипировку, вы устанавливаете Родителя на ноль.Таким образом, вы знаете, что всегда будет один элемент, вы просто контролируете, когда он виден.

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...