Modifying PC User Input for XBox Compatibility

Hey there!
I have a ragdoll game that I’m making, and I want it to be available to XBox users, along with PC. The problem is, I can’t figure out how to get the user inputs to work simultaneously for both devices. Here’s my PC script:

local character = script.Parent
local humanoid = character.Humanoid

game:GetService("ContextActionService"):BindAction("Ragdoll me", function(_,input)
	if input == Enum.UserInputState.Begin then
		local setEnabled = humanoid:GetState() ~= Enum.HumanoidStateType.Physics
		humanoid:ChangeState(setEnabled and Enum.HumanoidStateType.Physics or Enum.HumanoidStateType.GettingUp)
		character.Animate.Disabled = setEnabled
		
		if setEnabled then
			for _,v in pairs(humanoid:GetPlayingAnimationTracks()) do
				v:Stop(0)
			end
		end
	end
end, false, Enum.KeyCode.R)

And I’ve tried changing the KeyCode to ButtonX, but that either works only for XBox players, or not at all. I’ve even tried making a duplicate script for XBox. Same result. Only one device could ragdoll.
Any and all help is appreciated!

How about you just use userinputservice and require the keycode to be whatever pc OR whatever for xbox?

I tried that before, but it would either work for PC or XBox, but not both.

Show me the script of how you implemented that just so I can make sure your actually doing it right

local character = script.Parent
local humanoid = character.Humanoid

game:GetService("ContextActionService"):BindAction("Ragdoll me", function(_,input)
	if input == Enum.UserInputState.Begin then
		local setEnabled = humanoid:GetState() ~= Enum.HumanoidStateType.Physics
		humanoid:ChangeState(setEnabled and Enum.HumanoidStateType.Physics or Enum.HumanoidStateType.GettingUp)
		character.Animate.Disabled = setEnabled

		if setEnabled then
			for _,v in pairs(humanoid:GetPlayingAnimationTracks()) do
				v:Stop(0)
			end
		end
	end
end, false, Enum.KeyCode.R or Enum.KeyCode.ButtonX)

Like I said try using userinput service like so

local character = script.Parent
local humanoid = character.Humanoid
local UIS = game:GetService("UserInputService")


UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		print("yes")
	elseif input.KeyCode == Enum.KeyCode.ButtonA then
		print("yes")
	end
end)

I added the code into those, but whenever I press the key, my avatar fell rigid, instead of ragdoll, free-limb. (If you could call it that, I suppose.)

But technically its working for both Xbox and PC soo that’s another problem youll have to fix :blush:

1 Like

BindAction accepts a tuple for the input types.
This means you can send multiple values, as the input argument, like this:

ContextActionService:BindAction("Toggle ragdoll", handleAction, false, Enum.KeyCode.R, Enum.KeyCode.ButtonX)

What I like to do is have a “keybinds” table, and then unpack it when binding an action.
This allows me to easily add multiple inputs, and know what are an said actions inputs by looking at the code.

Here’s an example of what I do:

local KEYBINDS = {
    Enum.KeyCode.R,
    Enum.KeyCode.ButtonX
}
ContextActionService:BindAction("Toggle ragdoll", handleAction, false, unpack(KEYBINDS))
1 Like