Help with this if statement

Script: image

I’m trying to figure out why the highlighted elseif statement wont work, but all of the others do.
It doesn’t return an error.
Can anyone help?

1 Like

The problem is in your logic.

You are comparing value.Value to true and false. The value can only be either true or false, and never a third option, which means it will never go on to the third part of your statement.

Also, the third part of your statement is kind of redundant, since we already know that value.Name will equal key in the very first if statement. You should move the call for InvalidBind after the very first if statement inside an ‘else’.

1 Like

It looks like you need to move the first end up 2 lines so it looks like

           Sucsess() --typo?
        end
    elseif value.Name ~= key then
        InvalidBind()
    end

Move the last elseif back one tab and you see what I mean.

How I would write it is:

   for i, value in pairs(boundfolder:GetDescendants()) do
      if value.Name == key then
         if value.Value == true then
            print(key.." Is already bound.")
            AlreadyBound()
         else
            print(key.." Is not bound.")
            BindMenu()
            Sucsess()
         end
      else
         InvalidBind()
      end
   end
end
1 Like

image
like this or how do you mean?

You are checking if the value’s name does not equal the key after you have already checked that it is equal to the key.
It should be like this:

if value.Name == key then
   --do the other if values
else
   InvalidBind()
end

Like this:

for i, value in pairs(boundfolder:GetDescendants()) do
	if value.Name == key then
		if value.Value == true then
			print(key.." is already bound.")
			AlreadyBound()
		else
			print(key.." is not bound.")
			BindMenu()
			Sucsess()
		end	
	else
		InvalidBind()
	end
end

This would work normally yes but in this script it needs to first check if the name is in the list of values then if that values value is set to true or not.

Then you would use FindFirstChild if I’m understanding you correctly.

No because I have to check through all of the ones in a folder that dont yet exist at the scripts creation.
So this is why I use the for loop with getDescendants.

This doesnt work it freezes the script which keeps it from going on.

There is a recursive value in FindFirstChild, meaning it goes through descendants and not just the children.

local KeyFound = boundfolder:FindFirstChild(key, true)
if KeyFound then
   --do whatever
else
   InvalidBind()
end

This wouldn’t work as the value of key is constantly changing.

Did you try my solution above yet?

It also needs the abilty to be able to return a different value if the name is true but the value is false

That wouldnt work already tried that.

Then explain your problem in more detail. You just needed help with an if statement and that’s about it, no context or anything.

It would be easier if you saw the game so here it is. https://www.roblox.com/games/4484644276/keybind

You are not doing anything if the value.Name ~= key.
You are checking to see if the value.Name is key. Then inside the same if statement (when we already know it is key) , you are checking to see if it is not key which will never happen. You need an end to close the if value.Value statement before you move on to the logic of the name being wrong.

1 Like