function Reset()down={}state={}end function GetState()local E=UserInputService:GetGamepadState(GAMEPAD1)for E,S in pairs(E)do state[S.KeyCode]=S end end function InputBegan(E,S)if E.UserInputType==GAMEPAD1 then down[E.KeyCode]=true buttonDown:Fire(E.KeyCode)end end function InputEnded(E,S)if E.UserInputType==GAMEPAD1 then down[E.KeyCode]=false buttonUp:Fire(E.KeyCode)end end function InputChanged(E,S)if E.UserInputType==GAMEPAD1 then changed:Fire(E.KeyCode)end end function GamepadConnected(E)if E==GAMEPAD1 then Gamepad.IsConnected=true GetState()connected:Fire()end end function GamepadDisconnected(E)if E==GAMEPAD1 then Gamepad.IsConnected=false Reset()disconnected:Fire()end end

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

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

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

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

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

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

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