Why doesnt the script detect "ButtonA" on vr

like as i said on the title this code doesnt detect the “ButtonA”
i did it “ButtonB” to see if the error is in the code but it worked with no problem

local UIS = game:GetService("UserInputService")
local Player = game:FindFirstChild("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local L2anim = Animator:LoadAnimation(script.L2)
local L1anim = Animator:LoadAnimation(script.L1)
local R2anim = Animator:LoadAnimation(script.R2)
local R1anim = Animator:LoadAnimation(script.R1)
local ButtonXanim = Animator:LoadAnimation(script.ButtonX)
local ButtonAanim = Animator:LoadAnimation(script.ButtonA)

local L2 = false
local L1 = false
local R2 = false
local R1 = false
local ButtonX = false
local ButtonA = false


UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.ButtonL2 and not gameProcessedEvent then
		if L2 == false then
		   L2anim:Play()
		   L2 = true
			print("1")
		end
	end
end)
UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.ButtonL2 and not gameProcessedEvent then
		if L2 == true then
			L2anim:Stop()
			L2 = false
		end
	end
end)

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.ButtonL1 and not gameProcessedEvent then
		if L1 == false then
			L1anim:Play()
			L1 = true
			print("2")
		end
	end
end)
UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.ButtonL1 and not gameProcessedEvent then
		if L1 == true then
			L1anim:Stop()
			L1 = false
		end
	end
end)

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.ButtonR2 and not gameProcessedEvent then
		if R2 == false then
			R2anim:Play()
			R2 = true
			print("1")
		end
	end
end)
UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.ButtonR2 and not gameProcessedEvent then
		if R2 == true then
			R2anim:Stop()
			R2 = false
		end
	end
end)

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.ButtonR1 and not gameProcessedEvent then
		if R1 == false then
			R1anim:Play()
			R1 = true
			print("2")
		end
	end
end)
UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.ButtonR1 and not gameProcessedEvent then
		if R1 == true then
			R1anim:Stop()
			R1 = false
		end
	end
end)

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.ButtonX and not gameProcessedEvent then
		if ButtonX == false then
			ButtonXanim:Play()
			ButtonX = true
			print("2")
		end
	end
end)
UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.ButtonX and not gameProcessedEvent then
		if ButtonX == true then
			ButtonXanim:Stop()
			ButtonX = false
		end
	end
end)

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.ButtonB and not gameProcessedEvent then
		if ButtonA == false then
			ButtonAanim:Play()
			ButtonA	 = true
			print("2")
		end
	end
end)
UIS.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.ButtonB and not gameProcessedEvent then
		if ButtonA == true then
			ButtonAanim:Stop()
			ButtonA = false
		end
	end
end)
3 Likes

Quite strange that B works but A doesn’t. Maybe it’s something to do with the type of your controller? Try printing input and press A to see what happens.

Also, there is no reason to have a separate InputBegan and InputEnded connection for every different KeyCode. Just put it all under one and use elseif. Or even better, put all these KeyCodes and Animations in a table and just check the table on input like this:

local tbl = {

    ["A"] = ButtonAanim;
    ["X"] = ButtonXanim;

}

UIS.InputBegan:Connect(function(input, gpe)
    local animation = tbl[input.KeyCode.Name]
    if not animation or gpe then return end
    
    animation:Play()
end)

Do the same with input ended.

Also, I believe there is no reason for you to be using the variables L2, L1, R2 etc. It doesn’t matter if you call stop on an animation that isn’t playing

1 Like

im gonen usethem for grabbing pressing and some tings that need to use l1 l2 etc so how can i use button a on the vr emulator when i press a it plays animation but doestnt stop and stuck like this but my button a is working without a problem so whats the problem with the code or the game itself i guess its keybind to jump