Roblox Gui Toggles On/Off only on the First click - user-interface

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.

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
)

Remove a screenGUI when unequipping

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)

MS Access Display images based on combo box selection in Report

I'm currently creating hazard labels for a chemical inventory. I want the selected pictogram image to show up based on the data from the combo box for each of the records in the report.
My inital method was to place the images on top of each other and hide them. Then have their respective images be visible due to the combo box selection. This works perfectly in a form in the Form_Current event. When you place it in Report_Current event only the selected record image would change when clicked but all the rest are blank.
The code:
If GHS_Selection_1.Value = "1" Then
expos.Visible = False
flamme.Visible = False
rondflam.Visible = False
bottle.Visible = False
skull.Visible = False
exclam.Visible = False
acid.Visible = False
silhouete.Visible = False
aquatic.Visible = False
ElseIf GHS_Selection_1.Value = "2" Then
expos.Visible = True
flamme.Visible = False
rondflam.Visible = False
bottle.Visible = False
skull.Visible = False
exclam.Visible = False
acid.Visible = False
silhouete.Visible = False
aquatic.Visible = False
ElseIf GHS_Selection_1.Value = "3" Then
expos.Visible = False
flamme.Visible = True
rondflam.Visible = False
bottle.Visible = False
skull.Visible = False
exclam.Visible = False
acid.Visible = False
silhouete.Visible = False
aquatic.Visible = False
...etc
Is there a better method for this or improvements? OLE objects or unbounded images boxes? Any advice would be appreciated. Thanks.
Edit:
In a somewhat related issue, is there a way I can fill a whole page or many pages with the same record, and another when you select a range of chemicals for printing from a subform/table from a click of a button.

Roblox - How would I make a script that closes an open GUI when another is opened?

I have two GUI's that are opened by a button each at the top of the screen, but I want to make it so that if someone tries to open the second GUI with the first open, it will close the first one before opening the second one.
You can do something like:
local frames = {
[buttonA] = frameA;
[buttonB] = frameB;
}
for button,frame in pairs(frames) do
button.MouseButton1Click:connect(function()
if frame.Visible then
-- If we try to reopen the current frame, close it
frame.Visible = false
return
end
-- Close all frames and make ours visible
for k,v in pairs(frames) do
-- 'v == frame' is true if it's our frame
v.Visible = v == frame
end
end)
end
You should also check out the ROBLOX Wiki. It has some nice tutorials for Lua and stuff like opening/closing GUIs.
100% WORKS
To make an opening and closing gui...Put this script:
FIRSTGUINAME is your first gui, rename it and SECONDGUINAME is your second gui name so...
FIRSTGUINAME = script.Parent -- very important classifying info
SECONDGUINAME = script.Parent.Parent.Parent:WaitForChild(YOUR SECOND GUI)
FIRSTGUINAME.MouseButton1Click:connect(function()
SECONDGUINAME.Visible = not Visible
Thats all, now just copy paste this and your set
If you want to close a GUI if another one is open you can try this code:
GUI1 = (insert)
GUI2 = (insert)
GUI.MouseButton1Click:connect(function()
if GUI1.Visible == false then
if GUI2.Visible == true then
GUI2.Visible = false
GUI1.Visible = true
else
GUI.Visible = true
end
else
GUI1.Visible = false
end
If you are into the fancy stuff with the tweens, you might have to do that research yourself.

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