local T=Enum.UserInputType.Gamepad1 local G=Vector3.new()local o=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local H=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local y=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local R=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local f=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local N={}local j={}local v={IsConnected=(game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")):GetGamepadConnected(T);ButtonDown=o.Event,ButtonUp=H.Event,Changed=y.Event;Connected=R.Event,Disconnected=f.Event}function v.IsDown(G,T)return N[T]==true end function v.GetInput(G,T)return j[T]end function v.GetPosition(o,T)local H=o:GetInput(T)if H then return H.Position end return G end function Reset()N={}j={}end function GetState()local G=(game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")):GetGamepadState(T)for T,G in pairs(G)do j[G.KeyCode]=G end end function InputBegan(G,H)if G.UserInputType==T then N[G.KeyCode]=true o:Fire(G.KeyCode)end end function InputEnded(G,o)if G.UserInputType==T then N[G.KeyCode]=false H:Fire(G.KeyCode)end end function InputChanged(G,o)if G.UserInputType==T then y:Fire(G.KeyCode)end end function GamepadConnected(G)if G==T then v.IsConnected=true GetState()R:Fire()end end function GamepadDisconnected(G)if G==T then v.IsConnected=false Reset()f: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 v.IsConnected then GetState()end return v



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
