Code only works when it is wrong?

I’ve restarted roblox studio and tested it in a published experience, and it only works whenever the code is incorrect.

its supposed to work like this:

userinputservice.InputBegan:Connect(function(input)
  if input.KeyCode == Enum.KeyCode.C then
    if crouchinput == false then
      crouchinput = true
    else
      crouchinput = false
    end
    print(crouchinput)
  end
end)

but only works like this:

userinputservice.InputBegan:Connect(function(input)
  if input.KeyCode == Enum.KeyCode.C then
    if crouchinput == false then
      crouchinput = true
      crouchinput = false
    else
  
    end
    print(crouchinput)
  end
end)

output with correct code:

output with incorrect code:

its very odd to me and I do not know what to do, anything is appreciated.

this is literally how all coding works afaik

1 Like

i made another section of my code for crawling the exact same as the correct code and it works, yet it doesn’t for crouching

Can you please add print statements within the if statement, so we can see which parts are being triggered?

The incorrect code should not be creating that output, so it’s possible that the changes you make to the script aren’t saving.

2 Likes

userinputservice.InputBegan:Connect(function(input)
  if input.KeyCode == Enum.KeyCode.C then
    if crouchinput == false then
      crouchinput = true
      print(crouchinput)
    else
      crouchinput = false
      print(crouchinput)
    end
  end
end)

edit: on each input it prints both true and false at the same time

You can condense this if statement:

if crouchinput == false then
    crouchinput = true
else
   crouchinput = false
end

into this one line:

crouchinput = not crouchinput

Regardless, the first bit of code should not be giving you the first bit of output at all; it should be the other way around. Are you sure you’re disabling the other section of code when running the game?

1 Like

Can you please check if crouchinput is being updated elsewhere in the code?
Can you please also add a print just before the InputBegan line, to see how many times it gets setup?

1 Like

actually i beleive i had another copy of the code somewhere else, i appreciate the help