I made this code to only accept certain codes when it is put into a TextBox
and press the Submit button. The problem is, no matter what is put into the box, the button always outputs "accepted".
local reward = nil script.Parent.MouseButton1Click:connect(function() if script.Parent.Parent.Input.Text == 'Sneaky' or 'sneaky' then script.Parent.Text = "Accepted" wait(5) script.Parent.Text = "Submit" else script.Parent.Parent.Input.Text = "" script.Parent.Text = "Declined" wait(1) script.Parent.Text = "Submit" end end)
I noticed a few things about your code;
In your "not working" script, on line 3, you have said ..."Sneaky" or "sneaky", but what you want to check is if script.Parent.Parent.Input.Text is either "Sneaky" or "sneaky", therefore what you would want to do is this:
if script.Parent.Parent.Input.Text == "Sneaky" or script.Parent.Parent.Input.Text == "sneaky" then
Second of all, on line 7, you would rather check with an elseif
statement rather than else
,
so it would be something like thison line 7:
elseif script.Parent.Parent.Input.Text ~= "Sneaky" and script.Parent.Parent.Input.Text ~= "sneaky" then
You have to make sure you put and
, not or
because you want to check if the text is NOT "Sneaky" and the text is NOT "sneaky".
Here is the fixed code:
local reward = nil script.Parent.MouseButton1Click:connect(function() if script.Parent.Parent.Input.Text == "Sneaky" or script.Parent.Parent.Input.Text == "sneaky" then --Checks if the text is either "Sneaky" or "sneaky" script.Parent.Text = "Accepted" wait(5) script.Parent.Text = "Submit" elseif script.Parent.Parent.Input.Text ~= "Sneaky" and script.Parent.Parent.Input.Text ~= "sneaky" then --And if it is not "Sneaky" and it is not "sneaky" then it rejects the code script.Parent.Parent.Input.Text = "" script.Parent.Text = "Declined" wait(1) script.Parent.Text = "Submit" end end)
There is also ways you can shorten this code, by adding shortened version of script.Parent.Parent etc
, which would be something like this:
local reward = nil local Verify = script.Parent local Input = script.Parent.Parent.Input Verify.MouseButton1Click:connect(function() if Input.Text == "Sneaky" or Input.Text == "sneaky" then Verify.Text = "Accepted" wait(5) Verify.Text = "Submit" elseif Input.Text ~= "Sneaky" and Input.Text ~= "sneaky" then Input.Text = "" Verify.Text = "Declined" wait(1) Verify.Text = "Submit" end end)
If this helped you, make sure you accept my answer!
Hey, I'll just be adding a few points to @nanaluk01 's answer now.
Lets take his script now, since he pretty much fixed the code...
local reward = nil script.Parent.MouseButton1Click:connect(function() if script.Parent.Parent.Input.Text == "Sneaky" or script.Parent.Parent.Input.Text == "sneaky" then --Checks if the text is either "Sneaky" or "sneaky" script.Parent.Text = "Accepted" wait(5) script.Parent.Text = "Submit" elseif script.Parent.Parent.Input.Text ~= "Sneaky" and script.Parent.Parent.Input.Text ~= "sneaky" then --And if it is not "Sneaky" and it is not "sneaky" then it rejects the code script.Parent.Parent.Input.Text = "" script.Parent.Text = "Declined" wait(1) script.Parent.Text = "Submit" end end)
Seems cool right? Let's dig some more gems...
On line 3, ever heard of variables? :) You are trying to rewrite a ton of code again and again. It's alright! people make such mistakes, so don't feel bad and lets get on with this.
Maybe we could change the code like this?
local inputbox = script.Parent.Parent.Input script.Parent.MouseButton1Click:connect(function() if inputbox.Text == "Sneaky" or inputbox.Text == "sneaky" then -- Use variables to make things more clear and easier for ya :D script.Parent.Text = "Accepted" wait(5) script.Parent.Text = "Submit" elseif inputbox.Text ~= "Sneaky" and inputbox.Text ~= "sneaky" then -- It gets easy :) script.Parent.Parent.Input.Text = "" script.Parent.Text = "Declined" wait(1) script.Parent.Text = "Submit" end end)
ALRIGHTY! That seems nice, but wait, there's still something! You seem to kinda make things a bit large with exaggerating the code... Let's buckle it down a little. :3
local inputbox = script.Parent.Parent.Input script.Parent.MouseButton1Click:connect(function() if string.lower(inputbox.Text) == string.lower() then -- Use variables to make things more clear and easier for ya :D script.Parent.Text = "Accepted" wait(5) script.Parent.Text = "Submit" elseif inputbox.Text ~= "Sneaky" and inputbox.Text ~= "sneaky" then -- It gets easy :) script.Parent.Parent.Input.Text = "" script.Parent.Text = "Declined" wait(1) script.Parent.Text = "Submit" end end)
Whoo! We did it! :D
So, what does this string.lower("some STRING here")
do?
It basically turns the given string to lower case, so we don't have to deal with checking the string multiple times like "Sneaky", "sneaky". This is very useful function!
Learn more about it (and other functions) here ! http://wiki.roblox.com/index.php?title=Global_namespace/String_manipulation#string.lower
Okay, now this code can also get squeezed a bit more, but why should I do all the stuff when you're around to finish it! :D
Hope I helped !