Making a frame visible is possible but making invisible is not - lua

script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.Frame.Visible == true then
script.Parent.Parent.Frame.Visible = false
end
if script.Parent.Parent.Frame.Visible == false then
script.Parent.Parent.Frame.Visible = true
end
end)
I am very new to coding (have started practically yesterday ) and ive decided to start with lua in roblox studio. The program here is working i can assure you (some things in code may not be in proper place since im having a bit trouble writing code on this site) but the code works. I have tried to make it seperatly for 2 buttons then I tried this and nothing seems to work. I am starting to think it is caused by the program itself and not the code since I can make other things invisible with that code. I've placed those 2 codes for visibility and invisibility in 1 thing since I've been told that events caused by the code happen at same time so some of them may not work.

Assume Visible is true. The first condition will be true and Visible will be set to false. Then, in the next condition Visible is false and the condition is therefore true, again. And it sets Visible back to true.
Now, in order to fix it you want to execute the second condition only if the first failed. Take a look at elseif https://www.lua.org/pil/4.3.1.html.
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.Frame.Visible == true then
script.Parent.Parent.Frame.Visible = false
elseif script.Parent.Parent.Frame.Visible == false then
script.Parent.Parent.Frame.Visible = true
end
end)
This code is still quite bulky and can be improved:
script.Parent.MouseButton1Click:Connect(function()
script.Parent.Parent.Frame.Visible = not script.Parent.Parent.Frame.Visible
end)
Now you assign true to Visible if Visible does not evaluate to true. It basically toggles the boolean.

Related

Getting a ProximityPrompt to connect to a intvalue

I'm currently working on getting this proximity prompt so when you click E on it, it'll change the number value on the UI system. I'm a little confused because when I set it all up it does not seem to be changing the number at all.
Code Connected to PP:
script.Parent.Triggered:Connect(function()
game.ServerStorage.genact.Value = game.ServerStorage.genact.Value + 1
end)
Code Connected to UI:
while true do
script.Parent.Text = ("Generators Activated: ")..game.ServerStorage.genact.Value
end
Any thoughts to fix this? I also have a intvalue inside the ServerStorage which these connect to, but it doesnt seem to change its value from 0
Your issue is that the while loop never yields. It loops endlessly until it times out.
A better solution is to use the Changed signal on your IntValue. You use it to update the text input when it changes.
local genact = game.ServerStorage.genact
local textBox = script.Parent
genact.Changed:Connect(function(newVal)
textBox.Text = "Generators Activated: " .. tostring(newVal)
end)

Roblox shop not showing

This is for my game. I have all the code right with no errors.
script.Parent.MouseButton1Click:Connect(function()
game.StarterGui.ScreenGui.Enabled = true
script.Parent.Visible = false
end)
But when I start the game the code wont work properly, only the shop button disappears which is: "script.Parent.Visible = false" and the show me the shop: "game.StarterGui.ScreenGui.Enabled = true" is not working,and wont show up the shop, yes I have the ScreenGui disabled, and there are no output errors.
you need to make sure that you are useing the local StarterGui
when you do game.StarterGui.ScreenGui.Enabled = true you are changing the global instance and not the local. To fix this you need to use script.Parent and not game.StarterGui.
Below is some example code:
script.Parent.MouseButton1Click:Connect(function()
script.Parent.%how ever many parents it takes go get back to StarterGui%.ScreenGui.Enabled = true
script.Parent.Visible = false
end)
In the second line, You are enabling a GUI that's located in the StarterGui not the PlayerGui, To fix the problem, Change your code to this:
script.Parent.MouseButton1Click:Connect(function()
game.Players.LocalPlayer.PlayerGui.ScreenGui.Enabled = true
script.Parent.Visible = false
end)
Also, when posting code next time, use {} button instead

ROBLOX - If statement won't run inside of while loop?

I've run into something really strange. I was originally trying to make a music script (more like system, tbh) and it had the same problem. Now I have boiled it down to a simple test:
while true do
if script.Parent.musicstate.Value == true then
print("Play")
elseif script.Parent.musicstate.Value == false then
print("Stop")
end
wait()
end
When I run it, it will either only print "Stop" or only print "Play." The string it will print will depend on the value of the bool value when the game starts, as in it does not change what it prints depending on the current value. All help will be appreciated, thanks!
The structure, SongScript is what is running:
Never ever make such an infinite loop crashing your whole system! There is a lot more efficient way to do. You have the problem due to the fact that the script would auto break so it won't crash your whole system.
script.Parent.musicstate.Changed:Connect(function()
if script.Parent.musicstate.Value == true then
print("Play")
elseif script.Parent.musicstate.Value == false then
print("Stop")
end
end)

How can My GUI appear when my noob is killed?

[My code is :]
local function MWin()
game.StarterGui.ScreenGui1.DemonWin.Visible = true
if game.Workspace.Mages_Boss.Humanoid.Died:connect(function()
print("good")
end
[My noob is named : Mages_Boss
And my screen gui is named : DemonWin
I dont know what to put for "print("good")".]
First, modifying the starter GUI does nothing. You need to change the one player or all players with a for loop. In my answer, I’ll use the former with a player named ROBLOX. If you want to start with it invisible, you need game.Players.ROBLOX.PlayerGui.ScreenGui1.DemonWin.Visible = false. To make it visible on the death event, use game.Players.ROBLOX.PlayerGui.ScreenGui1.DemonWin.Visible = true.
Try using
DemonWin.Enabled=true;
or
DemonWin.Enabled=false;
to toggle whether it is active or not. I suppose in the died function, use the latter.
I'm going to assume you're not firing the function so you can get rid of that. You're also going to remove the .Died since it fires even when it's not dead. So your best bet would be to do also add a debounce- kind of function to your script. Here's the modified version:
game.StarterGui.ScreenGui1.DemonWin.Visible = false
if game.Workspace.Mages_Boss.Humanoid.Health == 0 then
game.StarterGui.ScreenGui1.DemonWin.Visible = true
else
game.StarterGui.ScreenGui1.DemonWin.Visible = false
end
Also, you would have to put the GUI in the StarterGui (located in game.Players.LocalPlayer.StarterGui) in order to not publicly malfunction this script.

LUA - OnUpdate shift modifier

I'm changing something in an Addon for World of Warcraft which are written in Lua.
There is a simple boolean variable which determines if some "all" frames are shown or only specific one.
So when = true then it will only show specific frames
when = false it will show all frames
I want to make a modifier with the shift key to show all frames when the shiftkey is pressed and hide them again when shift is realeased.
if IsShiftKeyDown() then
cfg.aura.onlyShowPlayer = false
else
cfg.aura.onlyShowPlayer = true
end
This is my very simple solution for it which works. The problem here is though it only works on starting of the script. You see in WoW everytime the interface gets loaded it will run the script if not told otherwise. That is not very efficent because I would send my user into a loadingscreen.
OnUpdate should fix my problem here which will run this specific code everytime a frame gets rendered which is pretty handy and is what I want to accomplish.
So this is what I made
local function onUpdate(self,elapsed)
if IsShiftKeyDown() then
cfg.aura.onlyShowPlayer = false
else
cfg.aura.onlyShowPlayer = true
end
end
local shiftdebuffs = CreateFrame("frame")
shiftdebuffs:SetScript("OnUpdate", onUpdate)
My problem is now that it doesn't work. I new to the onUpdate stuff and only copy pasted it from another addon I did which worked fine.
Right it goes straight to = false, which is only happening I think because it is the default.
thanks for the help
Right it goes straight to = false, which is only happening I think because it is the default.
No, there is no "default" branch in the if statement. For the control to get to the then branch the condition has to be evaluated to true. You need to check the logic, but if the script executed cfg.aura.onlyShowPlayer = false, it means that IsShiftKeyDown() was evaluated as true.

Resources