Xbox Controller UserInputService not Working

I am adding console support to my game… I’ve made a script so when you click right trigger it fires an event. Why is it not working?

UserInput.InputBegan:Connect(function(key)
	if key.UserInputType == Enum.UserInputType.Gamepad1 then
		if key.KeyCode == Enum.KeyCode.ButtonR2 then
			print("Clicked: "..key)
			if not InTween and not cooldown then
				InTween = true
				cooldown = true
				script.Parent:TweenSize(UDim2.new(0.141, 0,0.14, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
				script.Parent:TweenPosition(UDim2.new(0.459, 0,0.84, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
				local Anim =  game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Anims.Click)
				Anim:Play()
				click:Play()
				clickevent:FireServer(game.Players.LocalPlayer, 1)
				wait(0.2)
				script.Parent:TweenSize(UDim2.new(0.171, 0,0.165, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
				script.Parent:TweenPosition(UDim2.new(0.447, 0,0.813, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
				wait(0.2)
				InTween = false
				cooldown = false
			end
		end
	end
end)
1 Like

Does it print?

Nope, I’ve tested multiple times.

Try moving your print call higher, maybe that can provide better clues.

UserInput.InputBegan:Connect(function(key)
    -- key is an InputObject, so print its details:
    print(key.UserInputType, key.KeyCode, key.UserInputState)
    -- ...
end)

Remember that InputBegan also has a second parameter, gameProcessedEvent.

PS. You might find it better to use ContextActionService instead.

I moved the print statement higher up but it still won’t print…

UserInput.InputBegan:Connect(function(key, GPE)
	print("Clicked: "..key)
	if key.KeyCode == Enum.KeyCode.ButtonR2 then
		if not InTween and not cooldown then
			InTween = true
			cooldown = true
			script.Parent:TweenSize(UDim2.new(0.141, 0,0.14, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
			script.Parent:TweenPosition(UDim2.new(0.459, 0,0.84, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
			local Anim =  game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Anims.Click)
			Anim:Play()
			click:Play()
			clickevent:FireServer(game.Players.LocalPlayer, 1)
			wait(0.2)
			script.Parent:TweenSize(UDim2.new(0.171, 0,0.165, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
			script.Parent:TweenPosition(UDim2.new(0.447, 0,0.813, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
			wait(0.2)
			InTween = false
			cooldown = false
		end
	end
end)

key is an InputObject - it cannot be concatenated into a string, as this produces an error: attempt to concatenate string with Instance. However you can provide it as another argument to print. Try doing print("InputBegan", key, GPE)

Still didn’t work… I am really confused.

That means either your code isn’t running, or the event isn’t firing. It’s at this point you should find out if the connection is even being made, as it’s highly unlikely that InputBegan isn’t firing. Where’s the rest of your code? (Asking because I don’t see a UserInput = anywhere)

Would you like me to send the ENTIRE script for my button? If so I can do that!

Believe me, intentionally leaving out parts of your code often makes getting help with it harder than it needs to be. You can make a collapsible section using [details] on the devforum if you think it’ll be too long:

[details="code"]
```
code
```
[/details]

To make:

code
foo bar
1 Like

Imma just send the whole script for xbox related things/needed things…

Local Variables
local InTween = false

local sounds = game:GetService("ReplicatedStorage").SFX

local click = sounds:WaitForChild("Click")

local cooldown = false

local events = game:GetService("ReplicatedStorage").Events

local clickevent = events:WaitForChild("Click")

local Player = game.Players.LocalPlayer

local leaderstats = Player:WaitForChild("Leaderstats")

local StorageLvl = leaderstats:WaitForChild("StorageLvl")

local Clicks = leaderstats:WaitForChild("Clicks")

local Market = game:GetService("MarketplaceService")

local DoubleId = 25801028

local UserInput = game:GetService("UserInputService")

local xbox = "rbxassetid://8200686025"

local pc = "rbxassetid://8183712388"

Script/Function
while wait(1) do
	if UserInput.GamepadEnabled then
		script.Parent.Image = xbox
	else
		script.Parent.Image = pc
	end
end

UserInput.GamepadConnected:Connect(function()
	script.Parent.Image = xbox
end)

UserInput.GamepadDisconnected:Connect(function()
	script.Parent.Image = pc
end)

UserInput.InputBegan:Connect(function(key, GPE)
	print("InputBegan", key, GPE)
	if key.KeyCode == Enum.KeyCode.ButtonR2 then
		if not InTween and not cooldown then
			InTween = true
			cooldown = true
			script.Parent:TweenSize(UDim2.new(0.141, 0,0.14, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
			script.Parent:TweenPosition(UDim2.new(0.459, 0,0.84, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
			local Anim =  game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Anims.Click)
			Anim:Play()
			click:Play()
			clickevent:FireServer(game.Players.LocalPlayer, 1)
			wait(0.2)
			script.Parent:TweenSize(UDim2.new(0.171, 0,0.165, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
			script.Parent:TweenPosition(UDim2.new(0.447, 0,0.813, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
			wait(0.2)
			InTween = false
			cooldown = false
		end
	end
end)

There’s your problem. Because this loop never ends unless wait(1) returns false or nil or whatever, none of the code following the loop will ever run. Move the loop to the end of the script if you absolutely need it.

Well there it is. You have a while wait(1) do ... end loop preceeding the event connection, which means your loop never breaks and therefore your code never gets down that far.

You should use an event-based solution to updating the image based on GamepadEnabled. Something like GetPropertyChangedSignal("GamepadEnabled")

Edit: Also, using while wait() do ... end loops is a bad idea. See #2 on this listicle I wrote: Your Code Sucks: Improving Your Roblox Lua Coding Habits – Ozzy's Blog (PS the title is in jest, don’t take it personally!)

1 Like

OML Thank you SO much! I don’t usually use loops but it’s now nice to know that loops do that! Thank you all! Srry for wasting ur time.

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local AnimsFolder = script:WaitForChild("Anims")
local ClickAnimation = AnimsFolder:WaitForChild("Click")
local ClickAnim = Humanoid:LoadAnimation(ClickAnimation)
local ClickSound = script:WaitForChild("Click")

local UIS = game:GetService("UserInputService")
local InTween = nil
local Debounce = false
local GuiObject = script.Parent

local RS = game:GetService("ReplicatedStorage")
local RE = RS:WaitForChild("ClickEvent") --change to name of remoteevent instance

UIS.InputBegan:Connect(function(key, proc)
	if Debounce or InTween then
		return
	end
	if proc then
		return
	end
	
	if key.KeyCode == Enum.KeyCode.ButtonR2 then
		task.wait()
		Debounce = true
		InTween = true
		GuiObject:TweenSize(UDim2.new(0.141, 0,0.14, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
		GuiObject:TweenPosition(UDim2.new(0.459, 0,0.84, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
		ClickAnim:Play()
		ClickSound:Play()
		RE:FireServer(1)
		task.wait(0.2)
		GuiObject:TweenSize(UDim2.new(0.171, 0,0.165, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
		GuiObject:TweenPosition(UDim2.new(0.447, 0,0.813, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Elastic, 0.1, true)
		task.wait(0.2)
		Debounce = false
		InTween = false
	end
end)

Can I see the local script? I see you’re calling FireServer(), you don’t need to pass the player object when calling FireServer() as the player object is transmitted to the server automatically.

I’ve made various fixes here and improvements/optimisations.

You may need to fix some references as I made a few up (to get rid of blue lines).

1 Like