Remove a screenGUI when unequipping - user-interface

I already made a question on cooldown a weapon and I made a screenGUI to show the player when they can shoot again implementing the same debounce code. The problem is I've got no clue on how to delete the screengui/textlabel from the screen. As every tool I'm planing on doing has its own GUI, if the screenGUI of one tool doesn't delete, it will overlap with the same tool's GUI/ other tools GUI.
I already tried hiding the text label as stated in this question like this
player.PlayerGui.ScreenGui.TextLabel.Visible = false but
1) It only makes it disappear first time its unequipped and
2) Im afraid that given it doesn't get deleted, but rather hidden, after some time, stacked hidden GUIs will somehow affect the games smoothness in some way.
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)
I just need explanation on how to delete the screenGUI that contains the textlabel shown to the player when they unequip the weapon

The easiest way to handle this is to keep a reference to the UIElement when you first create it. Then when you equip the tool, you simply set the Parent. When you unequip, you set the Parent to nil. This way, you know that there will always be one element, you are simply controlling when it is visible.
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)

Related

How to make a GUI fade in roblox studio?

Hello roblox studio scripters,
I'm a intermediate scripter-builder and need some help with the gui for my game.
I have a start screen with a play button like this:
I'm trying to fade out the gui when the button is clicked, but none of the tutorials worked. This is my script for the button:
local button = script.Parent
local gui = script.Parent.Parent.Parent
button.MouseButton1Down:Connect(function()
gui.Enabled = false
end)
I don't know how to do the changing, would it be BackgroundTransparency? How would you change the transparency from 0 to 1 in 0.01 increments?
I tried to make the gui fade with a for loop, changing the BackgroundTransparency but that didn't work, this is that code:
local button = script.Parent
local gui = script.Parent.Parent.Parent
button.MouseButton1Down:Connect(function()
for i = 0, 100, 1 do
gui.Frame.BackgroundTransparency + 0.01
wait(0.01)
gui.Enabled = false
end
end)
I don't know why it isn't working.
If I have a typo or something, please tell me.
Thanks!
The loop solution has a few typos, here it is fixed:
local button = script.Parent
local gui = script.Parent.Parent.Parent
button.MouseButton1Down:Connect(function()
for i = 0, 100 do
gui.Frame.BackgroundTransparency += 0.01 -- += adds 0.01 each time
task.wait(0.01) -- better than wait(0.01)
end
gui.Enabled = false
end)
However, this is not an ideal solution. A better system would use Roblox's TweenService to change the gui's transparency. Tweens are less jittery, are easier to modify, and have lots of customisation properties including repeating, changing length of time, and style of easing (e.g. going faster at first, then slower near the end; see Easing Styles on the Roblox docs).
local TweenService = game:GetService("TweenService")
local button = script.Parent
local gui = script.Parent.Parent.Parent
local tweenInfo = TweenInfo.new(
2, -- Time
Enum.EasingStyle.Linear, -- Easing Style
Enum.EasingDirection.Out -- Easing Direction
-- See https://create.roblox.com/docs/reference/engine/datatypes/TweenInfo for more available properties
)
local tween = TweenService:Create(
gui.Frame, -- Instance to tween
tweenInfo, -- TweenInfo
{ Transparency = 1 } -- What we want to change
)
button.MouseButton1Down:Connect(function()
tween:Play()
tween.Completed:Wait() -- Wait until tween is complete
gui.Enabled = false
end)
Though both of these solutions change only the transparency of the background, so the child elements, such as the Playbutton, will stay visible until the gui is disabled. You may wish to replace the Frame with a CanvasGroup, which also changes the transparency of its children when its GroupTransparency property is changed.
local tween = TweenService:Create(
gui.CanvasGroup, -- Instance to tween
tweenInfo, -- TweenInfo
{ GroupTransparency = 1 } -- What we want to change
)

How to send alert at a certain time

I'd like to write a pine-script that will send me a report of the stock, says, it's pivot points, rsi, etc ... at 7am every morning and 3pm when mrkt close. I know how to do the alert, it's the trigger is I don't know how to tell it to send me the alert at 7am and 3pm.
Any thought on how to accomplish this? I look into timestamp or time, but don't know how to make that condition to send the alert.
Thanks
There are multiple ways of doing it, one of them is to create a session from time x to time y and get the start and the end of it.
I added comments for each section to see how it's working.
//#version=4
study("My Script", overlay = true)
//Sessions
session = input(title="Session", type=input.session, defval="0930-1555")
//Check if it's new bars
is_newbar2(sess) =>
t = time("D", sess)
na(t[1]) and not na(t) or t[1] < t
//Check if it's in session
is_session(sess) =>
not na(time(timeframe.period, sess))
//Call the function
Session = is_session(session)
//Plot the background color to see the session
bgcolor(Session ? color.new(color.aqua, 95) : na)
//Start and end of the session
start = Session and not Session[1]
end = (not Session) and Session[1]
//Plot the start and the end of the session
plotshape(start, style=shape.labeldown, color = color.aqua, text = "Start", textcolor = color.black, size = size.small)
plotshape(end, style=shape.labeldown, color = color.purple, text = "End", textcolor = color.black, size = size.small)
//Alerts
if start
alert("text alert", alert.freq_once_per_bar)
if end
alert("text alert", alert.freq_once_per_bar_close)
It's very important to write the starting of the session (if it start at 7am, then write exactly 07:00) and the time from the last candle of the session (if you are in 5m chart and the session expire at 2pm, write 13:55, if you are in 1H chart write 13:00...and so on), that's because after the end of the session, Tradingview will block any functions...since the market is closed, so we need to get the last bar of the session.
For the alerts, you just put your text and it's ready to go.

How can I make a GUI that shows how many bricks i have collected?

Each brick in my game has a value, and it gets added to leaderstats. But, I want a GUI to show how many BRICKS they have collected. For example, 2 bricks in my game are worth 32 points to be stored in leaderstats. Instead of showing the total, 64, i want it to show the amount of bricks i collected: 2.
Here is the code that collects the bricks and stores them in leaderstats:
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
if db == true then
db = false
script.Parent.Transparency = 1
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.leaderstats.ElectoralVotes.Value = player.leaderstats.ElectoralVotes.Value + 37.5
script.Sound:Play()
wait(1)
script.Parent:Remove()
end
end
end)
I want to keep the leaderstats in the top right, but on the screen i also want it to show the amount of bricks collected. Does anyone know how i could implement this into my game?
make a Main GUi and insert an TextLabel inside it insert Int value and a script and put the MainGUI in The Starter Gui Pack
next type the below code in the script if TextLabel:
local my_text_gui = script.Parent.TextLabel
local brick_count = my_gui.IntValue
local function change_value()
my_text_gui.Text = brick_count.Value
brick_count:GetPropertyChangedSignal("Value"):Connect(change_value())
Then change the touch detection script and add a value adding code given bellow(works only when the touch detection script is not a local script)
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
if db == true then
db = false
script.Parent.Transparency = 1
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.leaderstats.ElectoralVotes.Value = player.leaderstats.ElectoralVotes.Value + 37.5
hit.Parent.PlayerGui.Your_Brick_Counter_GUI.IntValue = hit.Parent.PlayerGui.Your_Brick_Counter_GUI.IntValue + 1
script.Sound:Play()
wait(1)
script.Parent:Remove()
end
end
end)
Thanks!

Roblox Gui Toggles On/Off only on the First click

My gui turns on when touching an NPC. It shows an "Off" button. When the off button is clicked the first time, it turns off the gui by setting the frame.Visible = false and the gui.Enabled = false.
When I touch the NPC again, the gui shows, as it should. Though the button doesn't cause the properties to be set to false. I have outputted the value of frame.Visible and it prints "False", though in the Properties window, the value is shown to be "True".
What is going on here?
--NPC Script
local soldier = game.Workspace["Level6"].Soldier.Humanoid.RootPart
local player = game.Players.LocalPlayer
local function onTouch(touchPart)
if touchPart.Parent:FindFirstChild("Humanoid") then
local gui = game.Players.LocalPlayer.PlayerGui.EndScreenGui
local frame = gui.Frame
frame.Visible = true
gui.Enabled = true
print("On")
end
end
soldier.Touched:Connect(onTouch)
And here is my gui code:
local button = script.Parent
function onClicked()
local frame = button.Parent
local gui = frame.Parent
--frame.Visible = false
gui.Enabled = false
print(frame.Visible)
end
button.MouseButton1Click:Connect(onClicked)
If we do syntax higlighting we can see this:
function onClicked()
local frame = button.Parent
local gui = frame.Parent
--frame.Visible = false
gui.Enabled = false
print(frame.Visible)
end
The frame.Visible line is commented off, so it doesn't run. Remove the -- before that line and your script should be all good.

How to make buttons stay pressed using corona

I am trying to get my buttons to stay "pressed" once it is released. Right now I am using the improved Buttons Module for corona and I have the default image being the button looking unpressed, and the over image being replaced by an image that looks pressed.
What I am trying to do is once the button is pressed, it stays on the over image. Here is how my code is set up for the button I am testing it on.
local digButton = buttons.newButton{
default = "digButton.png",
over = "digButtonPressed.png",
onEvent = digButtonFunction,
id = "dig"
}
digButton:setReferencePoint(display.CenterReferencePoint)
digButton.x = display.contentWidth/5
digButton.y = display.contentHeight/1.9
Also, I have a function (digButtonFunction) that sets the id of this button to a variable to be used to run an if statement for when the user pushes a button following this one.
This sounds to me like what you really want is a switch. Buttons are not really designed from a UI perspective to do that. The down-state is there just to give feedback to the user that some action happened.
If it were me, I'd not use the button bit at all, but load in to images using display.newImageRect() and draw the downstate first, then the upstate. Built a touch event listener on each one that will hide one or the other. I do this in my games for my sound on/off buttons.
local soundOn = true
local soundOnBtn, soundOffBtn
local function soundToggle(event)
if soundOn then
soundOn = false
soundOnBtn.isVisible = false
soundOffBtn.isVisible = true
else
soundOn = true
soundOnBtn.isVisible = true
soundOffBtn.isVisible = false
end
return true
end
soundOnBtn = display.newImageRect("images/switch_on.png", 46, 36)
soundOnBtn.x = display.contentWidth / 2 + 25
soundOnBtn.y = display.contentHeight / 2 - 15
group:insert(soundOnBtn)
soundOnBtn:addEventListener("tap", soundToggle)
soundOffBtn = display.newImageRect("images/switch_off.png", 46, 36)
soundOffBtn.x = display.contentWidth / 2 + 25
soundOffBtn.y = display.contentHeight / 2 - 15
group:insert(soundOffBtn)
soundOffBtn:addEventListener("tap", soundToggle)
soundOffBtn.isVisible = false

Resources