local y=Enum.UserInputType.Gamepad1 local J=Vector3.new()local K=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local l=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local d=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local C=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local E=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local c={}local D={}local W={IsConnected=(game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")):GetGamepadConnected(y),ButtonDown=K.Event,ButtonUp=l.Event,Changed=d.Event;Connected=C.Event;Disconnected=E.Event}function W.IsDown(J,y)return c[y]==true end function W.GetInput(J,y)return D[y]end function W.GetPosition(K,y)local l=K:GetInput(y)if l then return l.Position end return J end function Reset()c={}D={}end function GetState()local J=(game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")):GetGamepadState(y)for y,J in pairs(J)do D[J.KeyCode]=J end end function InputBegan(J,l)if J.UserInputType==y then c[J.KeyCode]=true K:Fire(J.KeyCode)end end function InputEnded(J,K)if J.UserInputType==y then c[J.KeyCode]=false l:Fire(J.KeyCode)end end function InputChanged(J,K)if J.UserInputType==y then d:Fire(J.KeyCode)end end function GamepadConnected(J)if J==y then W.IsConnected=true GetState()C:Fire()end end function GamepadDisconnected(J)if J==y then W.IsConnected=false Reset()E:Fire()end end;(game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")).InputBegan:Connect(InputBegan);(game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")).InputEnded:Connect(InputEnded);(game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")).InputChanged:Connect(InputChanged);(game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")).GamepadConnected:Connect(GamepadConnected);(game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")).GamepadDisconnected:Connect(GamepadDisconnected)if W.IsConnected then GetState()end return W
--[[
	
	FIELDS:
	
		Gamepad.IsConnected           > true|false
	
	
	METHODS:
	
		Gamepad:IsDown(keyCode)       > true|false
		Gamepad:GetInput(keyCode)     > returns InputObject associated with keycode (nil if none)
		Gamepad:GetPosition(keyCode)  > Vector3 (will return 0,0,0 if no keycode available)
	
	
	EVENTS:
	
		Gamepad.ButtonDown(keyCode)
		Gamepad.ButtonUp(keyCode)
		Gamepad.Changed(keyCode)
		Gamepad.Connected()
		Gamepad.Disconnected()
	
--]]

local GAMEPAD1 = Enum.UserInputType.Gamepad1
local ZERO_VECTOR = Vector3.new()

local buttonDown = Instance.new("BindableEvent")
local buttonUp = Instance.new("BindableEvent")
local changed = Instance.new("BindableEvent")
local connected = Instance.new("BindableEvent")
local disconnected = Instance.new("BindableEvent")

local down = {}
local state = {}

local Gamepad = {
	IsConnected = game:GetService("UserInputService"):GetGamepadConnected(GAMEPAD1),
	ButtonDown = buttonDown.Event,
	ButtonUp = buttonUp.Event,
	Changed = changed.Event,
	Connected = connected.Event,
	Disconnected = disconnected.Event,
}

function Gamepad:IsDown(keyCode)
	return (down[keyCode] == true)
end

function Gamepad:GetInput(keyCode)
	return state[keyCode]
end

function Gamepad:GetPosition(keyCode)
	local input = self:GetInput(keyCode)
	if input then
		return input.Position
	end
	return ZERO_VECTOR
end

function Reset()
	down = {}
	state = {}
end

function GetState()
	-- Map KeyCodes to corresponding InputObjects on the gamepad:
	local s = game:GetService("UserInputService"):GetGamepadState(GAMEPAD1)
	for _, inputObj in pairs(s) do
		state[inputObj.KeyCode] = inputObj
	end
end

function InputBegan(input, processed)
	if input.UserInputType == GAMEPAD1 then
		down[input.KeyCode] = true
		buttonDown:Fire(input.KeyCode)
	end
end

function InputEnded(input, processed)
	if input.UserInputType == GAMEPAD1 then
		down[input.KeyCode] = false
		buttonUp:Fire(input.KeyCode)
	end
end

function InputChanged(input, processed)
	if input.UserInputType == GAMEPAD1 then
		changed:Fire(input.KeyCode)
	end
end

function GamepadConnected(gamepad)
	if gamepad == GAMEPAD1 then
		Gamepad.IsConnected = true
		GetState()
		connected:Fire()
	end
end

function GamepadDisconnected(gamepad)
	if gamepad == GAMEPAD1 then
		Gamepad.IsConnected = false
		Reset()
		disconnected:Fire()
	end
end

game:GetService("UserInputService").InputBegan:Connect(InputBegan)
game:GetService("UserInputService").InputEnded:Connect(InputEnded)
game:GetService("UserInputService").InputChanged:Connect(InputChanged)
game:GetService("UserInputService").GamepadConnected:Connect(GamepadConnected)
game:GetService("UserInputService").GamepadDisconnected:Connect(GamepadDisconnected)

if Gamepad.IsConnected then
	GetState()
end

return Gamepad
