Why isn't the Boolean value working?

What’s my Goal?

I want that after my IF STATEMENT Is TRUE To make a GUI Element to become visible.

What is the issue?

local function Question_Answer(Answer)
	local Input = game.Players.LocalPlayer.PlayerGui.ScreenGui.Screen.Input
	local Submit = game.Players.LocalPlayer.PlayerGui.ScreenGui.Screen.Submit
	
	Submit.MouseButton1Click:Connect(function()
		local Input_Lower = string.lower(tostring(Input.Text))
		
		if Input_Lower == Answer or Input_Lower:find(Answer) then
			game.StarterGui.ScreenGui.Screen.Correct.Visible = true
		end
		
	end)
	
end

Question_Answer('yes')

*Heres my script

game.StarterGui.ScreenGui.Screen.Correct.Visible = true

This part of the script doesn’t seem to work. :point_up_2:

Can you tell me the issue with the script and a possible solution? :thinking:

Edit:

Here’s the place file for the test game.
test.rbxl (44.1 KB)

try using prints after the so called IF STATEMENT and tell us if it prints or not

instead of
“Input_Lower == Answer or Input_Lower:find(Answer)”

try
v1:
(Input_Lower == Answer) or Input_Lower:find(Answer)

or try
v2:
(Input_Lower == Answer) or (Input_Lower:find(Answer)>0)

or
v3:
local b = (Input_Lower == Answer) or (Input_Lower:find(Answer)>0)
game.StarterGui.ScreenGui.Screen.Correct.Visible = b

In fact, I’m pretty sure that double check is not needed, so
v4:

game.StarterGui.ScreenGui.Screen.Correct.Visible = Input_Lower:find(Answer)>0

would be enough.

I can’t open Roblox now, this is why I “suggest” it only without test.

The code itself looks fine at a glance. I don’t think your issue lies within what you’ve shown. Also you don’t need to use tostring on a value that’s already a string.

Edit:
Actually, looking at it more closely, I wouldn’t recommend putting a click event inside of a function like that. If there’s only one submit button, which there seems to be, this will likely cause conflicts under certain circumstances.
I would recommend writing the function separately and then use it within the click event.
I feel like I’m missing a bit of information though to explain it properly.

This too:

The script is probably working as it should. In the troublesome line, you are setting the visibility of something in the starter gui, not in the player gui which is the one actually rendered in the client.

To avoid this kind of confusion, I’d define a variable screen on top of your code (ideally before declaring the function, but that depends on your setup). You should also consider calling WaitForChild instead of directly indexing stuff in the client.

I’d recommend using WaitForChild regardless of whether or not it’s the client. While loading issues are almost guaranteed on the client, it’s also an issue server-side as well. It’s just good practice regardless.

I don’t think always using WaitForChild can be considered good practice though. But I did not mean to say this function should only be used in the client.

About your edited post, that’s a good observation. While OP’s setup might be justifiable, there should definitely be some logic to disconnect the mouse event connection.

@abcanish123 I think this article may help you avoid memory leaks and undefined behavior: Handling Events

Ok i full modified your script, but then realised why the original script didn’t work lol.

the issue is this line:

game.StarterGui.ScreenGui.Screen.Correct.Visible = true

it should instead be

game.Players.LocalPlayer.PlayerGui.ScreenGui.Screen.Correct.Visible = true

my attempt at fixing it before realising the issue:

local Answer= 'yes'
local Submit = game.Players.LocalPlayer.PlayerGui.ScreenGui.Screen.Submit

local function Question_Answer()
	local Input = game.Players.LocalPlayer.PlayerGui.ScreenGui.Screen.Input
	local Input_Lower = string.lower(tostring(Input.Text))
		
	if Input_Lower == Answer or string.find(Input_Lower, Answer then
		game.Players.LocalPlayer.PlayerGui.ScreenGui.Screen.Correct.Visible = true
	end
end

Submit.MouseButton1Click:Connect(Question_Answer)

Well to clarify, I mean if it’s something you need to reference on game start. If you know it already exists, you obviously won’t need to use it in that case.

But on game start or cases where you can’t be 100% sure it already exists, I’d still recommend referencing it. If your concern is the slight delay that might occur, you could always just do:

local part = workspace.Part or workspace:WaitForChild('Part')

You’re probably getting the PlayerGui before the player loads in. You should use a “:WaitForChild” for the first local statement.

Its not that it is before it loads in, the issue is that OP calls StarterGui, not PlayerGui (as mentioned in my previous response to OP)

1 Like

Yes, I did that

I replaced this the IF statement definition to print("It Worked!") and as shown in the image the “It Worked” string printed out after I said “Yes” and clicked on the submit button.

This proves that the entire script is working fine except for this line of code-
game.StarterGui.ScreenGui.Screen.Correct.Visible = true

Yep! This works! Thanks, @SeargentAUS :grinning: