Table doesn't update properly

In my script, I require some values to be set in order to play the right animation, and I set the values through UserInputService depending whether or not a certain key is pressed

I have tried searching for help but have received none. The code I have written is a bit long so I will be simplifying it

local testtable = {
  item1 = false
  item2 = false
}

UserInputService.InputBegan:Connect(function(key)
  if key.KeyCode == "A" then
    testtable.item1 = true
  elseif key.KeyCode == "B" then
    testtable.item2 = true
  end
print(testtable)
end)

UserInputService.InputEnded:Connect(function(key)
  if key.KeyCode == "A" then
    testtable.item1 = false
  elseif key.KeyCode == "B" then
    print("BUnPressed")
    testtable.item2 = false
  end
print(testtable)
end)

if you press A and press B without letting go it prints:
{
“item1” = false
“item2” = true
}
BUnPressed only prints when I let go of the button, not before testtable prints

1 Like
local UserInputService = game:GetService("UserInputService")
local testtable = {
    item1 = false
    item2 = false
}

UserInputService.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.A then
        testtable.item1 = true
    elseif key.KeyCode == Enum.KeyCode.B then
        testtable.item2 = true
    end
    print(testtable)
end)

UserInputService.InputEnded:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.A then
        testtable.item1 = false
    elseif key.KeyCode == Enum.KeyCode.B then
        print("BUnPressed")
        testtable.item2 = false
    end
    print(testtable)
end)

The only part you messed up is when the player uses the Keybind it goes like this:

local UserInputService = game:GetService("UserInputService");


local testtable = {
	item1 = false,
	item2 = false
}

UserInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.A then
		testtable.item1 = true
	elseif key.KeyCode == Enum.KeyCode.B then
		testtable.item2 = true
	end
	print(testtable)
end)

UserInputService.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.A then
		testtable.item1 = false
	elseif key.KeyCode == Enum.KeyCode.B then
		print("BUnPressed")
		testtable.item2 = false
	end
	print(testtable)
end)

Thanks, but it still says only one of them is true if both are being pressed

Well, you placed it as inputended, perhaps just use one at a time, in different scripts should make clean